Skip to content

Commit f1d7fa8

Browse files
Merge pull request #21 from edgeimpulse/add-visual-ad
Add visual anomaly grid to image classification results
2 parents d31a726 + 5308ecd commit f1d7fa8

File tree

3 files changed

+17
-0
lines changed

3 files changed

+17
-0
lines changed

examples/image/classify-full-frame.py

100755100644
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,11 @@ def print_classification(res, tag):
119119
for bb in res["result"]["bounding_boxes"]:
120120
print('\t%s (%.2f): x=%d y=%d w=%d h=%d' % (bb['label'], bb['value'], bb['x'], bb['y'], bb['width'], bb['height']))
121121

122+
if "visual_anomaly_grid" in res["result"].keys():
123+
print('Found %d visual anomalies (%d ms.)' % (len(res["result"]["visual_anomaly_grid"]), res['timing']['dsp'] + res['timing']['classification']))
124+
for grid_cell in res["result"]["visual_anomaly_grid"]:
125+
print('\t%s (%.2f): x=%d y=%d w=%d h=%d' % (grid_cell['label'], grid_cell['value'], grid_cell['x'], grid_cell['y'], grid_cell['width'], grid_cell['height']))
126+
122127
print_classification(res_l, 'LEFT')
123128
print_classification(res_r, 'RIGHT')
124129

examples/image/classify-image.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@ def main(argv):
6868
print('\t%s (%.2f): x=%d y=%d w=%d h=%d' % (bb['label'], bb['value'], bb['x'], bb['y'], bb['width'], bb['height']))
6969
cropped = cv2.rectangle(cropped, (bb['x'], bb['y']), (bb['x'] + bb['width'], bb['y'] + bb['height']), (255, 0, 0), 1)
7070

71+
if "visual_anomaly_grid" in res["result"].keys():
72+
print('Found %d visual anomalies (%d ms.)' % (len(res["result"]["visual_anomaly_grid"]), res['timing']['dsp'] + res['timing']['classification']))
73+
for grid_cell in res["result"]["visual_anomaly_grid"]:
74+
print('\t%s (%.2f): x=%d y=%d w=%d h=%d' % (grid_cell['label'], grid_cell['value'], grid_cell['x'], grid_cell['y'], grid_cell['width'], grid_cell['height']))
75+
cropped = cv2.rectangle(cropped, (grid_cell['x'], grid_cell['y']), (grid_cell['x'] + grid_cell['width'], grid_cell['y'] + grid_cell['height']), (255, 125, 0), 1)
76+
7177
# the image will be resized and cropped, save a copy of the picture here
7278
# so you can see what's being passed into the classifier
7379
cv2.imwrite('debug.jpg', cv2.cvtColor(cropped, cv2.COLOR_RGB2BGR))

examples/image/classify-video.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,12 @@ def getFrame(sec):
9191
print('\t%s (%.2f): x=%d y=%d w=%d h=%d' % (bb['label'], bb['value'], bb['x'], bb['y'], bb['width'], bb['height']))
9292
img = cv2.rectangle(cropped, (bb['x'], bb['y']), (bb['x'] + bb['width'], bb['y'] + bb['height']), (255, 0, 0), 1)
9393

94+
if "visual_anomaly_grid" in res["result"].keys():
95+
print('Found %d visual anomalies (%d ms.)' % (len(res["result"]["visual_anomaly_grid"]), res['timing']['dsp'] + res['timing']['classification']))
96+
for grid_cell in res["result"]["visual_anomaly_grid"]:
97+
print('\t%s (%.2f): x=%d y=%d w=%d h=%d' % (grid_cell['label'], grid_cell['value'], grid_cell['x'], grid_cell['y'], grid_cell['width'], grid_cell['height']))
98+
img = cv2.rectangle(cropped, (grid_cell['x'], grid_cell['y']), (grid_cell['x'] + grid_cell['width'], grid_cell['y'] + grid_cell['height']), (255, 125, 0), 1)
99+
94100
if (show_camera):
95101
cv2.imshow('edgeimpulse', cv2.cvtColor(cropped, cv2.COLOR_RGB2BGR))
96102
if cv2.waitKey(1) == ord('q'):

0 commit comments

Comments
 (0)