이민규

add code

1 +import serial
2 +import cv2
3 +from time import sleep
4 +import numpy as np
5 +import ocr_image
6 +import findDB
7 +
8 +ser = serial.Serial('/dev/ttyAMA0',115200)
9 +if(ser.isOpen()):
10 + print("Serial Communication in operation")
11 +
12 +LiveCam = cv2.VideoCapture(0)
13 +YOLO_net = cv2.dnn.readNet('yolov3-tiny_best.weights','yolov3-tiny.cfg')
14 +
15 +classes = ['box']
16 +layer_names = YOLO_net.getLayerNames()
17 +output_layers = [layer_names[i[0] - 1] for i in YOLO_net.getUnconnectedOutLayers()]
18 +
19 +frame_num = 0
20 +count = 0
21 +while LiveCam.isOpened():
22 +
23 + ret, frame = LiveCam.read()
24 + if ret is False:
25 + print("No Video Input")
26 + break
27 + if frame_num != 20:
28 + frame_num += 1
29 + elif frame_num == 20:
30 + frame_num = 0
31 +
32 + h, w, c = frame.shape
33 +
34 + blob = cv2.dnn.blobFromImage(frame, 0.00392, (416, 416), (0, 0, 0), True, crop=False)
35 + YOLO_net.setInput(blob)
36 + outs = YOLO_net.forward(output_layers)
37 +
38 + class_ids = []
39 + confidences = []
40 + boxes = []
41 +
42 + for out in outs:
43 +
44 + for detection in out:
45 +
46 + scores = detection[5:]
47 + class_id = np.argmax(scores)
48 + confidence = scores[class_id]
49 +
50 + if confidence > 0.4:
51 + center_x = int(detection[0] * w)
52 + center_y = int(detection[1] * h)
53 + dw = int(detection[2] * w)
54 + dh = int(detection[3] * h)
55 + x = int(center_x - dw / 2)
56 + y = int(center_y - dh / 2)
57 + boxes.append([x, y, dw, dh])
58 + confidences.append(float(confidence))
59 + class_ids.append(class_id)
60 +
61 + indexes = cv2.dnn.NMSBoxes(boxes, confidences, 0.45, 0.4)
62 +
63 +
64 + if confidences :
65 + bestscore = confidences.index(max(confidences))
66 + best_x, best_y, best_w, best_h = boxes[bestscore]
67 +
68 + if best_x > 310 :
69 + print("오른쪽으로 이동")
70 + ser.write(serial.to_bytes([int('1',16)]))
71 +
72 + elif best_x + best_w < 330 :
73 + print("왼쪽으로 이동")
74 + ser.write(serial.to_bytes([int('2',16)]))
75 +
76 + else :
77 + print("직진")
78 + count = 1
79 +
80 +
81 + cv2.rectangle(frame, (best_x, best_y), (best_x + best_w, best_y + best_h), (0, 0, 255), 5)
82 + cv2.putText(frame, 'box', (best_x, best_y - 20), cv2.FONT_ITALIC, 0.5, (255, 255, 255), 1)
83 +
84 + cv2.imshow("YOLOv3", frame)
85 + if count == 1 :
86 + if cv2.waitKey(100) > 0 :
87 + cv2.imwrite('cap_img.jpg', frame)
88 + ser.write(serial.to_bytes([int('3',16)]))
89 + break
90 +
91 +
92 + if cv2.waitKey(100) > 0:
93 + break
94 +
95 +
96 +image = cv2.imread("cap_img.jpg")
97 +template = cv2.imread("myform.jpg")
98 +
99 +ocr_result = ocr_image.ocr(image, template)
100 +
101 +(name, result) = ocr_result["name"]
102 +(address, result) = ocr_result["address"]
103 +(detail_address, result) = ocr_result["detail_address"]
104 +
105 +name = name.replace(" ","")
106 +address = address.replace(" ","")
107 +detail_address = detail_address.replace(" ","")
108 +
109 +print(name)
110 +print(address)
111 +print(detail_address)
112 +
113 +name = findDB.find_name(name)
114 +detail_address = findDB.find_address(detail_address)
115 +
116 +destination_num = findDB.set_destination(name, detail_address)
117 +print (destination_num)
118 +
119 +sleep(5)
120 +
121 +while(1) :
122 + ser.write(serial.to_bytes([int('6',16)]))
123 +
124 +
125 +