opencv 如何仅在特定区域进行检测

pvabu6sv  于 2023-01-17  发布在  其他
关注(0)|答案(1)|浏览(167)

我正在做一个项目,尝试用yolo和easyocr进行物体检测和文本检测,因为我是一个初学者,对计算机视觉很陌生,如果有人能帮助我,我会很高兴。

密码如下

import cv2
import numpy as np
import easyocr

# Load Yolo
net = cv2.dnn.readNet('yolov4-tiny-custom_3000.weights', 'yolov4-tiny-custom.cfg')
classes = []
with open("obj.names", "r") as f:
    classes = [line.strip() for line in f.readlines()]
layer_names = net.getLayerNames()
output_layers = [layer_names[i - 1] for i in net.getUnconnectedOutLayers()]
colors = np.random.uniform(0, 255, size=(len(classes), 3))

cap = cv2.VideoCapture('car1.mp4')

# Declare Ocr
cascade_src = 'haarcascade_russian_plate_number.xml'
cascade = cv2.CascadeClassifier(cascade_src)
reader = easyocr.Reader(['en'], gpu = False)
# Declare Ocr
while True:
    _, frame = cap.read()
    height, width, channels = frame.shape
    #frame = cv2.resize(frame, (800, 600))

    # Yolo Detection
    # Detecting objects
    blob = cv2.dnn.blobFromImage(frame, 0.00392, (416, 416), (0, 0, 0), True, crop=False)
    net.setInput(blob)
    outs = net.forward(output_layers)

    # Showing informations on the screen
    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.5:
                # Object detected
                center_x = int(detection[0] * width)
                center_y = int(detection[1] * height)
                w = int(detection[2] * width)
                h = int(detection[3] * height)

                # Rectangle coordinates
                x = int(center_x - w / 2)
                y = int(center_y - h / 2)

                boxes.append([x, y, w, h])
                confidences.append(float(confidence))
                class_ids.append(class_id)

    indexes = cv2.dnn.NMSBoxes(boxes, confidences, 0.5, 0.4)
    for i in range(len(boxes)):
        if i in indexes:
            x, y, w, h = boxes[i]
            label = str(classes[class_ids[i]])
            color = colors[class_ids[i]]
            cv2.rectangle(frame, (x, y), (x + w, y + h), color, 2)
            cv2.putText(frame, label, (x, y + 30), cv2.FONT_HERSHEY_PLAIN, 3, color, 3)
            print("Jenis Mobil: " +label)

 # Text Reader Using Ocr
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    plate = cascade.detectMultiScale(gray, 1.1, 5)
    for x,y,w,h in plate:
        wT,hT,cT = frame.shape
        a,b = (int(0.02*wT),int(0.02*hT))
        plate2 = frame[y+a:y+h-a,x+b:x+w-b,:]
        
        cv2.rectangle(frame,(x,y),(x+w,y+h),(60,60,255),2)
        cv2.rectangle(frame,(x-1,y-40),(x+w+1,y),(60,60,255),-1)
        result = reader.readtext(plate2)
        for detek in result:
            top_left = (int(detek[0][0][0]), int(detek[0][0][1]))
            bottom_right = (int(detek[0][2][0]), int(detek[0][2][1]))
            text = detek[1]       
            cv2.putText(frame,text,(x,y-10),cv2.FONT_HERSHEY_SIMPLEX,0.9,(255,255,255),2)     
            print("Nomor Kendaran: " + text)
    # Text Reader Using Ocr
    
    cv2.imshow("Detection", frame)
    key = cv2.waitKey(1)
    if key == 27:
        break
cap.release()
cv2.destroyAllWindows()

我试着用ROI来检测物体,但是我做不到。请问有什么建议吗?

dy1byipe

dy1byipe1#

裁剪图像,然后再将其馈送到模型

while True:
    _, frame = cap.read()
   im_crop = im[y1:y2, x1:x2]  # set x1,x2,y1,y2 based on your ROI
   blob = cv2.dnn.blobFromImage(frame, 0.00392, (416, 416), (0, 0, 0), True, crop=False)

这将加快推理时间,并且模型要处理的数据更少

相关问题