forked from pjreddie/darknet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
67 lines (51 loc) · 1.63 KB
/
main.py
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
from pyDarknet.libpydarknet import DarknetObjectDetector
import cv2
import sys
specs = "cfg/yolo.cfg"
weights = "yolo.weights"
_net = DarknetObjectDetector(specs, weights)
to_string = 0.0
fordward = 0.0
def detect(im):
"""
Detect faces in the given image
:param img: Image for detect faces.
:type specs: numpy.ndarray
:return: List with bounding boxes (dlib rectangle).
:rtype: list
"""
global to_string, fordward
img = cv2.resize(im, (448, 448), interpolation = cv2.INTER_CUBIC)
img_t = img.transpose([2, 0, 1])
img_str = img_t.tostring()
dets = _net.detect_object(img_str, img_t.shape[2], # img h
img_t.shape[1], # img w
img_t.shape[0])
bboxs = []
scale_y = im.shape[0] / 448.0;
scale_x = im.shape[1] / 448.0;
print dets
for bb in dets:
print bb.confidence, bb.cls
if bb.confidence > 0.4:
bboxs.append(map(int, [bb.left * scale_x, bb.top * scale_y, bb.right * scale_x, bb.bottom * scale_y]))
return bboxs
im = cv2.imread(sys.argv[1])
bboxs = detect(im)
dest_path = "salida.jpg"
max_area = -1
max_bbox = None
for bb in bboxs:
area = (bb[2] - bb[0]) * (bb[3] - bb[1])
if max_area < area:
max_area = area
max_bbox = bb
for bb in bboxs:
area = (bb[2] - bb[0]) * (bb[3] - bb[1])
if max_area == area:
cv2.rectangle(im, (bb[0], bb[1]), (bb[2], bb[3]), (0,0,255))
else:
cv2.rectangle(im, (bb[0], bb[1]), (bb[2], bb[3]), (0,255,0))
bb = max_bbox
cv2.imwrite("paint.jpg", im)
cv2.imwrite(dest_path, im[bb[1]:bb[3], bb[0]:bb[2]])