-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
59 lines (41 loc) · 1.79 KB
/
test.py
File metadata and controls
59 lines (41 loc) · 1.79 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
import tensorflow as tf
import numpy as np
import cv2
import pathlib
interpreter = tf.lite.Interpreter(model_path="model.tflite")
labels = ['Monnalisa','Urlo di Munch','Notte Stellata','Bacio','Venere','Persistenza','Ragazza con Turbante','Guernica']
interpreter.allocate_tensors()
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()
for i,od in enumerate(output_details):
print(i,"\t",od,"\n")
def draw_rect(image, box,detected_class_idx):
y_min = int(max(1, (box[0] * 320)))
x_min = int(max(1, (box[1] * 320)))
y_max = int(min(320, (box[2] * 320)))
x_max = int(min(320, (box[3] * 320)))
# draw a rectangle on the image
cv2.rectangle(image, (x_min, y_min), (x_max, y_max), (0, 0, 255), 2)
cv2.putText(image,labels[int(detected_class_idx)], (x_min,y_min-15),cv2.FONT_HERSHEY_PLAIN,1,(0,0,255),2)
for file in pathlib.Path('images').iterdir():
if file.suffix != '.jpeg' and file.suffix != '.png' and file.suffix != '.jpg' and file.suffix != '.webp':
continue
img = cv2.imread(r"{}".format(file.resolve()))
new_img = cv2.resize(img, (320,320))
interpreter.set_tensor(input_details[0]['index'], [new_img])
interpreter.invoke()
rects = interpreter.get_tensor(
output_details[1]['index'])
scores = interpreter.get_tensor(
output_details[0]['index'])
classes = interpreter.get_tensor(
output_details[3]['index'])
print("scores:",scores,"\n")
print("boxes:",rects,"\n")
print("classes:",classes)
print(interpreter.get_tensor(output_details[0]['index']))
for index, score in enumerate(scores[0]):
if score > 0.5:
draw_rect(new_img,rects[0][index],classes[0][index])
cv2.imshow("image", new_img)
cv2.waitKey(0)