forked from cchighman/PriusWatchML
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage_test.py
More file actions
109 lines (82 loc) · 3.37 KB
/
Copy pathimage_test.py
File metadata and controls
109 lines (82 loc) · 3.37 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
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
import time
import json
import cv2
import numpy as np
import requests
from PIL import Image
from imageai.Detection import ObjectDetection
from imageai.Prediction.Custom import CustomImagePrediction
from prius_color import has_prius_color_from_array
def write_json(data, filename= "prius_results.json"):
with open(filename, "w") as f:
json.dump(data, f, indent=4)
def save_result(result):
with open("prius_results.json") as json_file:
data = json.load(json_file)
temp = data
temp.append(result)
write_json(data)
detector = ObjectDetection()
detector.setModelTypeAsYOLOv3()
detector.setModelPath("./yolo.h5")
detector.loadModel(detection_speed="fastest")
prediction = CustomImagePrediction()
prediction.setModelTypeAsResNet()
# self.prediction.setModelPath(model_path + "model_ex-012_acc-0.988819.h5")
prediction.setModelPath("./model_ex-043_acc-0.996787.h5")
prediction.setJsonPath("./model_class.json")
prediction.loadModel(prediction_speed="fastest")
custom_objects = detector.CustomObjects(car=True)
accuracy = 0
imgs = []
data = requests.get("http://seattle.gov/trafficcams/images/15_NW_65_NS.jpg").content
decoded = cv2.imdecode(np.frombuffer(data, np.uint8), -1)
imgs.append(decoded)
data = requests.get("http://seattle.gov/trafficcams/images/15_NW_65_NS.jpg").content
decoded = cv2.imdecode(np.frombuffer(data, np.uint8), -1)
imgs.append(decoded)
for decoded in imgs:
start1 = time.time()
result = detector.detectCustomObjectsFromImage(custom_objects=custom_objects,
input_type="array",
extract_detected_objects=True,
input_image=np.array(decoded),
output_type="array",
minimum_percentage_probability=50)
start2 = time.time()
print("Detection Time: " + str(start2 - start1))
detected = []
for arr in result[1]:
(x1, y1, x2, y2) = arr["box_points"]
img = decoded[y1:y2, x1:x2]
colorStart = time.time()
has_color = has_prius_color_from_array(img)
colorEnd = time.time()
print("has_color Time: " + str(colorEnd - colorStart))
if has_color is not True:
start2 = time.time()
predictions, probabilities = prediction.predictImage(img, input_type="array", result_count=2)
start3 = time.time()
print("Prediction Time: " + str(start3 - start2))
for eachPrediction, eachProbability in zip(predictions, probabilities):
if "prius" in eachPrediction and eachProbability > accuracy:
try:
success = {
#'timestamp': frame_time,
# 'image_name': frame_file,
# 'image_path': frame_dir,
# 'cam_id': str(cam['id']),
'probability': str(eachProbability)
#'predictor': 'series'
}
r = requests.post("http://priusvision.azurewebsites.net/api/PriusTrigger", data=json.dumps(success))
print("----> PRIUS IDENTIFIED. Data: " + str(success))
except Exception as e:
print("Saving Prius result failed: " + str(e))
save_result(success)
if r.status_code is not 200:
print("POST Failed. Saving manually.")
save_result(success)
with open("./test/img2.jpg", 'wb') as handler:
handler.write(data)
cv2.imwrite("./test/img1.jpg", img)