Skip to content

Commit d514aa2

Browse files
Run Yolo via python to classify photos
Superseded by jevois_yolo running on the Jevois cam device
1 parent 2ddc96a commit d514aa2

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

Diff for: sort_photos.py

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import os,sys
2+
import time
3+
sys.path.append('/home/peter/darknet/python')
4+
sys.path.append('/home/peter/darknet')
5+
6+
import darknet as dn
7+
import pdb
8+
import shutil
9+
import numpy as np
10+
import cv2
11+
12+
net = dn.load_net(b'/home/peter/darknet/cfg/pdq.cfg',b'/home/peter/darknet/pdq_3100.weights',0)
13+
meta = dn.load_meta(b'/home/peter/darknet/data/pdq_obj.data')
14+
15+
folder = '/home/peter/Pictures'
16+
17+
while True:
18+
files = os.listdir(folder)
19+
#dn.detect fails occasionally. I suspect a race condition.
20+
time.sleep(5)
21+
for f in files:
22+
if f.endswith(".jpg"):
23+
print (f)
24+
path = os.path.join(folder, f)
25+
pathb = path.encode('utf-8')
26+
res = dn.detect(net, meta, pathb)
27+
print (res) #list of name, probability, bounding box center x, center y, width, height
28+
i=0
29+
new_path = '/home/peter/Pictures/none/'+f #initialized to none
30+
img = cv2.imread(path,cv2.IMREAD_COLOR) #load image in cv2
31+
while i<len(res):
32+
res_type = res[i][0].decode('utf-8')
33+
if "person" in res_type:
34+
#copy file to person directory
35+
new_path = '/home/peter/Pictures/people/'+f
36+
#set the color for the person bounding box
37+
box_color = (0,255,0)
38+
elif "cat" in res_type:
39+
new_path = '/home/peter/Pictures/cat/'+f
40+
box_color = (0,255,255)
41+
elif "bird" in res_type:
42+
new_path = '/home/peter/Pictures/bird/'+f
43+
box_color = (255,0,0)
44+
elif "squirrel" in res_type:
45+
new_path = '/home/peter/Pictures/squirrel/'+f
46+
box_color = (0,0,255)
47+
#get bounding box
48+
center_x=int(res[i][2][0])
49+
center_y=int(res[i][2][1])
50+
width = int(res[i][2][2])
51+
height = int(res[i][2][3])
52+
53+
UL_x = int(center_x - width/2) #Upper Left corner X coord
54+
UL_y = int(center_y + height/2) #Upper left Y
55+
LR_x = int(center_x + width/2)
56+
LR_y = int(center_y - height/2)
57+
58+
#write bounding box to image
59+
cv2.rectangle(img,(UL_x,UL_y),(LR_x,LR_y),box_color,5)
60+
#put label on bounding box
61+
font = cv2.FONT_HERSHEY_SIMPLEX
62+
cv2.putText(img,res_type,(center_x,center_y),font,2,box_color,2,cv2.LINE_AA)
63+
i=i+1
64+
cv2.imwrite(new_path,img) #wait until all the objects are marked and then write out.
65+
#todo. This will end up being put in the last path that was found if there were multiple
66+
#it would be good to put it all the paths.
67+
os.remove(path) #remove the original
68+
69+

0 commit comments

Comments
 (0)