-
Notifications
You must be signed in to change notification settings - Fork 0
/
effDet_test.py
208 lines (128 loc) · 5.46 KB
/
effDet_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import torch
import torchvision.transforms as T
import numpy as np
import cv2
import urllib.request
import tensorflow as tf
import tensorflow as tf
import numpy as np
import cv2
import urllib.request
#import hub
import tensorflow_hub as hub
from effDet import EffDet
from PIL import Image, ImageDraw, ImageFont
# Open the image file
img = cv2.imread('img1.png')
h, w, c = img.shape
img=img[:,:,0:3]
img = np.uint8(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
img_batched = [img] # simulez batch
##################################### predictia ###################################
# Instantirre retea (de fapt inauntru nu e insttantiata reteaua, doar ca instantiez un obiect definit de mine)
eff_det = EffDet()
# incarcare model
eff_det.load_weights()
# aplic inferenta
boxes, class_ids = eff_det.inference(img_batched)
###################################################################################
# e nevoide sa transform img in PIL
img = Image.fromarray(img)
# Draw the bounding boxes and labels on the image
draw = ImageDraw.Draw(img)
font = ImageFont.truetype("arial.ttf", 16)
for box, label in zip(boxes, class_ids):
#draw.rectangle(box[0], outline="red")
#print([box[0]*h, box[1]*w, box[2]*h, box[3]*w])
#print(box[0])
draw.rectangle([box[1]*w, box[0]*h, box[3]*w, box[2]*h], outline="red")
#print(int(label-1))
draw.text((box[0]*h, box[1]*w), str(int(label)), font=font, fill="red")
# Display the image
img.show()
'''
# img to tensor
tensor = tf.convert_to_tensor(img_batched, dtype=tf.uint8) # se pare ca modelul cere sa fie uint8
# Incarcare retea
detector = hub.load(r"https://tfhub.dev/tensorflow/efficientdet/d6/1")
detector_output = detector(tensor)
class_ids = detector_output["detection_classes"]
boxes = detector_output["detection_boxes"]
#output_img = detector_output
###############################################################
# le transform in liste?
class_ids = class_ids.numpy().tolist()
boxes = boxes.numpy().tolist()
# e nevoide sa transform img in PIL
img = Image.fromarray(img)
# Draw the bounding boxes and labels on the image
draw = ImageDraw.Draw(img)
font = ImageFont.truetype("arial.ttf", 16)
for box, label in zip(boxes[0], class_ids[0]):
#draw.rectangle(box[0], outline="red")
#print([box[0]*h, box[1]*w, box[2]*h, box[3]*w])
#print(box[0])
draw.rectangle([box[1]*w, box[0]*h, box[3]*w, box[2]*h], outline="red")
print(int(label-1))
draw.text((box[0]*h, box[1]*w), str(class_labels[int(label)]), font=font, fill="red")
# Display the image
img.show()
'''
'''
import torch
import torchvision.transforms as T
import numpy as np
import cv2
import urllib.request
import tensorflow as tf
import tensorflow as tf
import numpy as np
import cv2
import urllib.request
#import hub
import tensorflow_hub as hub
from PIL import Image, ImageDraw, ImageFont
# Labels COCO_V1
class_labels = ['person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus', 'train', 'truck', 'boat', 'traffic light', 'fire hydrant', 'stop sign', 'parking meter', 'bench', 'bird', 'cat', 'dog', 'horse', 'sheep', 'cow', 'elephant', 'bear', 'zebra', 'giraffe', 'backpack', 'umbrella', 'handbag', 'tie', 'suitcase', 'frisbee', 'skis', 'snowboard', 'sports ball', 'kite', 'baseball bat', 'baseball glove', 'skateboard', 'surfboard', 'tennis racket', 'bottle', 'wine glass', 'cup', 'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple', 'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza', 'donut', 'cake', 'chair', 'couch', 'potted plant', 'bed', 'dining table', 'toilet', 'tv', 'laptop', 'mouse', 'remote', 'keyboard', 'cell phone', 'microwave', 'oven', 'toaster', 'sink', 'refrigerator', 'book', 'clock', 'vase', 'scissors', 'teddy bear', 'hair drier', 'toothbrush']
print(len(class_labels))
# Open the image file
img = cv2.imread('img1.png')
h, w, c = img.shape
img=img[:,:,0:3]
img = np.uint8(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
img_batched = [img] # simulez batch
#print(np.unique(img))
# Convert the image to a TensorFlow tensor
tensor = tf.convert_to_tensor(img_batched, dtype=tf.uint8) # se pare ca modelul cere sa fie uint8
# Print the shape of the tensor
#print(tensor)
#print(tensor.shape)
#print('kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk')
# Apply image detector on a single image.
detector = hub.load(r"https://tfhub.dev/tensorflow/efficientdet/d6/1")
detector_output = detector(tensor)
class_ids = detector_output["detection_classes"]
boxes = detector_output["detection_boxes"]
#print('lllllllllllllllllllllllllllllllllllllllllll')
#print(boxes)
#print(class_ids)
#output_img = detector_output
###############################################################
# le transform in liste?
class_ids = class_ids.numpy().tolist()
boxes = boxes.numpy().tolist()
# e nevoide sa transform img in PIL
img = Image.fromarray(img)
# Draw the bounding boxes and labels on the image
draw = ImageDraw.Draw(img)
font = ImageFont.truetype("arial.ttf", 16)
for box, label in zip(boxes[0], class_ids[0]):
#draw.rectangle(box[0], outline="red")
#print([box[0]*h, box[1]*w, box[2]*h, box[3]*w])
#print(box[0])
draw.rectangle([box[1]*w, box[0]*h, box[3]*w, box[2]*h], outline="red")
print(int(label-1))
draw.text((box[0]*h, box[1]*w), str(class_labels[int(label)]), font=font, fill="red")
# Display the image
img.show()
'''