main_controller.py
3.33 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import serial
import cv2
from time import sleep
import numpy as np
import ocr_image
import findDB
ser = serial.Serial('/dev/ttyAMA0',115200)
if(ser.isOpen()):
print("Serial Communication in operation")
LiveCam = cv2.VideoCapture(0)
YOLO_net = cv2.dnn.readNet('yolov3-tiny_best.weights','yolov3-tiny.cfg')
classes = ['box']
layer_names = YOLO_net.getLayerNames()
output_layers = [layer_names[i[0] - 1] for i in YOLO_net.getUnconnectedOutLayers()]
frame_num = 0
count = 0
while LiveCam.isOpened():
ret, frame = LiveCam.read()
if ret is False:
print("No Video Input")
break
if frame_num != 20:
frame_num += 1
elif frame_num == 20:
frame_num = 0
h, w, c = frame.shape
blob = cv2.dnn.blobFromImage(frame, 0.00392, (416, 416), (0, 0, 0), True, crop=False)
YOLO_net.setInput(blob)
outs = YOLO_net.forward(output_layers)
class_ids = []
confidences = []
boxes = []
for out in outs:
for detection in out:
scores = detection[5:]
class_id = np.argmax(scores)
confidence = scores[class_id]
if confidence > 0.4:
center_x = int(detection[0] * w)
center_y = int(detection[1] * h)
dw = int(detection[2] * w)
dh = int(detection[3] * h)
x = int(center_x - dw / 2)
y = int(center_y - dh / 2)
boxes.append([x, y, dw, dh])
confidences.append(float(confidence))
class_ids.append(class_id)
indexes = cv2.dnn.NMSBoxes(boxes, confidences, 0.45, 0.4)
if confidences :
bestscore = confidences.index(max(confidences))
best_x, best_y, best_w, best_h = boxes[bestscore]
if best_x > 310 :
print("오른쪽으로 이동")
ser.write(serial.to_bytes([int('1',16)]))
elif best_x + best_w < 330 :
print("왼쪽으로 이동")
ser.write(serial.to_bytes([int('2',16)]))
else :
print("직진")
count = 1
cv2.rectangle(frame, (best_x, best_y), (best_x + best_w, best_y + best_h), (0, 0, 255), 5)
cv2.putText(frame, 'box', (best_x, best_y - 20), cv2.FONT_ITALIC, 0.5, (255, 255, 255), 1)
cv2.imshow("YOLOv3", frame)
if count == 1 :
if cv2.waitKey(100) > 0 :
cv2.imwrite('cap_img.jpg', frame)
ser.write(serial.to_bytes([int('3',16)]))
break
if cv2.waitKey(100) > 0:
break
image = cv2.imread("cap_img.jpg")
template = cv2.imread("myform.jpg")
ocr_result = ocr_image.ocr(image, template)
(name, result) = ocr_result["name"]
(address, result) = ocr_result["address"]
(detail_address, result) = ocr_result["detail_address"]
name = name.replace(" ","")
address = address.replace(" ","")
detail_address = detail_address.replace(" ","")
print(name)
print(address)
print(detail_address)
name = findDB.find_name(name)
detail_address = findDB.find_address(detail_address)
destination_num = findDB.set_destination(name, detail_address)
print (destination_num)
sleep(5)
while(1) :
ser.write(serial.to_bytes([int('6',16)]))