-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcell13.py
More file actions
72 lines (64 loc) · 3.06 KB
/
cell13.py
File metadata and controls
72 lines (64 loc) · 3.06 KB
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
for i,data in enumerate(data_loader_test):
# get the image file name for predictions file name
image_name = 'image no:' + str(int(data[1][0]['image_id']))
model_image = data[0][0]
cv2_image = np.transpose(model_image.numpy()*255,(1, 2, 0)).astype(np.float32)
cv2_image = cv2.cvtColor(cv2_image, cv2.COLOR_RGB2BGR).astype(np.float32)
# add batch dimension
model_image = torch.unsqueeze(model_image, 0)
start_time = time.time()
with torch.no_grad():
outputs = model(model_image.to(device))
end_time = time.time()
# get the current fps
fps = 1 / (end_time - start_time)
# add `fps` to `total_fps`
total_fps += fps
# increment frame count
frame_count += 1
# load all detection to CPU for further operations
outputs = [{k: v.to('cpu') for k, v in t.items()} for t in outputs]
# carry further only if there's detected boxes
if len(outputs[0]['boxes']) != 0:
boxes = outputs[0]['boxes'].data.numpy()
scores = outputs[0]['scores'].data.numpy()
# filter out boxes according to `detection_threshold`
boxes = boxes[scores >= detection_threshold].astype(np.int32)
scores = np.round(scores[scores >= detection_threshold],2)
draw_boxes = boxes.copy()
# draw the bounding boxes and write the class name on top of it
for j,box in enumerate(draw_boxes):
cv2.rectangle(cv2_image,
(int(box[0]), int(box[1])),
(int(box[2]), int(box[3])),
color_inference, 2)
cv2.putText(img=cv2_image, text="Crater",
org=(int(box[0]), int(box[1] - 5)),
fontFace=cv2.FONT_HERSHEY_SIMPLEX,fontScale= 0.3,color= color_inference,
thickness=1, lineType=cv2.LINE_AA)
cv2.putText(img=cv2_image, text=str(scores[j]),
org=(int(box[0]), int(box[1] + 8)),
fontFace=cv2.FONT_HERSHEY_SIMPLEX,fontScale= 0.3,color= color_inference,
thickness=1, lineType=cv2.LINE_AA)
# add boxes for labels
for box in data[1][0]['boxes']:
cv2.rectangle(cv2_image,
(int(box[0]), int(box[1])),
(int(box[2]), int(box[3])),
color_label, 2)
cv2.putText(img=cv2_image, text="Label",
org=(int(box[0]), int(box[1] - 5)),
fontFace=cv2.FONT_HERSHEY_SIMPLEX,fontScale= 0.3,color= color_label,thickness=1, lineType=cv2.LINE_AA)
# set size
plt.figure(figsize=(10,10))
plt.axis("off")
# convert color from CV2 BGR back to RGB
plt_image = cv2.cvtColor(cv2_image/255.0, cv2.COLOR_BGR2RGB)
plt.imshow(plt_image)
plt.show()
cv2.imwrite(f"./results/{image_name}.jpg", cv2_image)
print(f"Image {i + 1} done...")
print('-' * 50)
print('TEST PREDICTIONS COMPLETE')
avg_fps = total_fps / frame_count
print(f"Average FPS: {avg_fps:.3f}")