Professional Documents
Culture Documents
Nhận Diện Xe Đạp
Nhận Diện Xe Đạp
import numpy as np
import math
START_POINT = 150
END_POINT = 80
CLASSES = ["person", "bicycle", "car", "motorbike"]
# Define vehicle class
VEHICLE_CLASSES = [1]
YOLOV3_CFG = 'yolov3-tiny.cfg'
YOLOV3_WEIGHT = 'yolov3-tiny.weights'
CONFIDENCE_SETTING = 0.4
YOLOV3_WIDTH = 416
YOLOV3_HEIGHT = 416
MAX_DISTANCE = 60
def get_output_layers(net):
"""
--
Nhận các lớp đầu ra của darknet
: param net: Mô hình
: return: output_layers
"""
try:
layer_names = net.getLayerNames()
output_layers = [layer_names[i[0] - 1] for i in
net.getUnconnectedOutLayers()]
return output_layers
except:
print("Can't get output layers")
return None
boxes = []
class_ids = []
confidences = []
for out in layer_output:
for detection in out:
scores = detection[5:]
class_id = np.argmax(scores)
confidence = scores[class_id]
if confidence > confidence_setting and class_id in
VEHICLE_CLASSES:
print("Object name: " + classes[class_id] + " - Confidence:
{:0.2f}".format(confidence * 100))
center_x = int(detection[0] * frame_w)
center_y = int(detection[1] * frame_h)
w = int(detection[2] * frame_w)
h = int(detection[3] * frame_h)
x = center_x - w / 2
y = center_y - h / 2
class_ids.append(class_id)
confidences.append(float(confidence))
boxes.append([int(x), int(y), int(w), int(h)])
return boxes, class_ids, confidences
if number_frame % skip_frame == 0:
# Detect object and check new object
boxes, class_ids, confidences = detections_yolo3(net, frame,
CONFIDENCE_SETTING, YOLOV3_WIDTH,
YOLOV3_HEIGHT,
width, height, classes=CLASSES)
for idx, box in enumerate(boxes):
box_x, box_y, box_width, box_height = box
if not check_location(box_y, box_height, height):
# This object doesnt pass the end line
box_center_x = int(box_x + box_width / 2.0)
box_center_y = int(box_y + box_height / 2.0)
check_new_object = True
for tracker in list_object:
# Check exist object
current_box_x, current_box_y, current_box_width,
current_box_height = tracker['box']
current_box_center_x = int(current_box_x +
current_box_width / 2.0)
current_box_center_y = int(current_box_y +
current_box_height / 2.0)
# Calculate distance between 2 object
distance = math.sqrt((box_center_x -
current_box_center_x) ** 2 +
(box_center_y -
current_box_center_y) ** 2)
if distance < MAX_DISTANCE:
# Object is existed
check_new_object = False
break
if check_new_object and check_start_line(box_y,
box_height):
# Append new object to list
new_tracker = cv2.TrackerKCF_create()
new_tracker.init(frame, tuple(box))
new_object = {
'id': class_ids[idx],
'tracker': new_tracker,
'confidence': confidences[idx],
'box': box
}
list_object.append(new_object)
# Draw new object
draw_prediction(CLASSES, colors, frame,
new_object['id'], new_object['confidence'],
box_x, box_y, box_width,
box_height)
# Put summary text
cv2.putText(frame, "Number : {:03d}".format(number_vehicle), (10,
30), cv2.FONT_HERSHEY_SIMPLEX, 0.5,
(0, 0, 255), 2)
# Draw start line
cv2.line(frame, (0, START_POINT), (width , START_POINT), (204, 90,
208), 1)
# Draw end line
cv2.line(frame, (0, height - END_POINT), (width, height -
END_POINT), (255, 0, 0), 2)
# Show frame
cv2.imshow("Counting", frame)
key = cv2.waitKey(1) & 0xFF
if key == ord('q'):
break
out.write(frame)
cap.release()
out.release()
cv2.destroyAllWindows()
if __name__ == '__main__':
counting_vehicle('dc.mp4', 'vehicles.avi')