|
a |
|
b/predict_HRI.py |
|
|
1 |
import cv2 |
|
|
2 |
from darkflow.net.build import TFNet |
|
|
3 |
|
|
|
4 |
options = {'model': 'cfg/tiny-yolo-voc-3c.cfg', |
|
|
5 |
'load': 3750, |
|
|
6 |
'threshold': 0.15, |
|
|
7 |
'gpu': 0.7} |
|
|
8 |
|
|
|
9 |
tfnet = TFNet(options) |
|
|
10 |
|
|
|
11 |
C = [] # Center |
|
|
12 |
R = [] # Radius |
|
|
13 |
L = [] # Label |
|
|
14 |
|
|
|
15 |
im_name = 'HRI001' |
|
|
16 |
image = cv2.imread('data/' + im_name + '.jpg') |
|
|
17 |
|
|
|
18 |
for h in range(0, 2592, 890): |
|
|
19 |
for w in range(0, 3872, 1290): |
|
|
20 |
im = image[h:h + 890, w:w + 1290] |
|
|
21 |
output = tfnet.return_predict(im) |
|
|
22 |
|
|
|
23 |
RBC = 0 |
|
|
24 |
WBC = 0 |
|
|
25 |
Platelets = 0 |
|
|
26 |
|
|
|
27 |
for prediction in output: |
|
|
28 |
label = prediction['label'] |
|
|
29 |
confidence = prediction['confidence'] |
|
|
30 |
|
|
|
31 |
tl = (prediction['topleft']['x'], prediction['topleft']['y']) |
|
|
32 |
br = (prediction['bottomright']['x'], prediction['bottomright']['y']) |
|
|
33 |
|
|
|
34 |
height, width, _ = image.shape |
|
|
35 |
center_x = int((tl[0] + br[0]) / 2) |
|
|
36 |
center_y = int((tl[1] + br[1]) / 2) |
|
|
37 |
center = (center_x + w, center_y + h) |
|
|
38 |
radius = int((br[0] - tl[0]) / 2) |
|
|
39 |
|
|
|
40 |
C.append(center) |
|
|
41 |
R.append(radius) |
|
|
42 |
L.append(label) |
|
|
43 |
|
|
|
44 |
record = [] |
|
|
45 |
|
|
|
46 |
for i in range(0, len(C)): |
|
|
47 |
center = C[i] |
|
|
48 |
radius = R[i] |
|
|
49 |
label = L[i] |
|
|
50 |
|
|
|
51 |
if label == 'RBC': |
|
|
52 |
color = (255, 0, 0) |
|
|
53 |
elif label == 'WBC': |
|
|
54 |
color = (0, 255, 0) |
|
|
55 |
elif label == 'Platelets': |
|
|
56 |
color = (0, 0, 255) |
|
|
57 |
|
|
|
58 |
image = cv2.circle(image, center, radius, color, 5) |
|
|
59 |
font = cv2.FONT_HERSHEY_COMPLEX |
|
|
60 |
image = cv2.putText(image, label, (center[0] - 30, center[1] + 10), font, 1, color, 2) |
|
|
61 |
|
|
|
62 |
cv2.imwrite('output/' + im_name + 'out.jpg', image) |