(Yolov4 테스트) Yolov4 모델이 출력한 검사 상자를 원도에 비추기
논리:
4
4
4
4
4
import os
import cv2
import darknet
config_file = './train/data/yolov4.cfg'
data_file = "./train/data/train.data"
weights = "./backup/yolov4_last.weights"
def image_detection(image_path, network, class_names, class_colors, thresh):
# Darknet doesn't accept numpy images.
# Create one with image we reuse for each detect
width = darknet.network_width(network)
height = darknet.network_height(network)
darknet_image = darknet.make_image(width, height, 3)
image = cv2.imread(image_path)
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image_resized = cv2.resize(image_rgb, (width, height),
interpolation=cv2.INTER_LINEAR)
darknet.copy_image_from_bytes(darknet_image, image_resized.tobytes())
detections = darknet.detect_image(network, class_names, darknet_image, thresh=thresh)
darknet.free_image(darknet_image)
image = darknet.draw_boxes(detections, image_resized, class_colors)
return cv2.cvtColor(image, cv2.COLOR_BGR2RGB), detections
network, class_names, class_colors = darknet.load_network(config_file,
data_file,
weights,
batch_size=1)
def bbox2points(bbox):
x, y, w, h = bbox
xmin = int(round(x - (w / 2)))
xmax = int(round(x + (w / 2)))
ymin = int(round(y - (h / 2)))
ymax = int(round(y + (h / 2)))
return xmin, ymin, xmax, ymax
colors = {
'truck': (0, 0, 255), 'car': (0, 255, 0), 'people': (255, 0, 0)}
save_path = "save"
images_path = "test_demo1"
img_names = os.listdir(images_path)
for img_name in img_names:
img = os.path.join(images_path, img_name)
image, detections = image_detection(img, network, class_names, class_colors, thresh=0.25)
im = cv2.imread(img)
w = im.shape[1]
h = im.shape[0]
for label, confidence, bbox in detections:
xmin, ymin, xmax, ymax = bbox2points(bbox)
xmin = int(float((xmin * w) / 608))
xmax = int(float((xmax * w) / 608))
ymin = int(float((ymin * h) / 608))
ymax = int(float((ymax * h) / 608))
cv2.rectangle(im, (xmin, ymax), (xmax, ymin), colors[label], 1)
cv2.putText(im, "{} [{:.2f}]".format(label, float(confidence)),
(xmin, ymax - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.5,
colors[label], 2)
cv2.imwrite(os.path.join(save_path, img_name + '__.jpg'), im)
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.