简体   繁体   English

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

[英]How do i make detection only on spesific area

I'm working on a project trying to do object detection and text detection using both yolo and easyocr.我正在做一个项目,试图使用 yolo 和 easyocr 进行 object 检测和文本检测。 Since I'm a beginner and really new to computer vision, I would be glad if someone can help me.由于我是初学者并且对计算机视觉真的很陌生,如果有人能帮助我,我会很高兴。

Here's the code:这是代码:

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()

I am trying to use ROI to detect the object but I am not able to do it.我正在尝试使用 ROI 来检测 object,但我做不到。 Any advice please?有什么建议吗?

Crop the image before it is fed to the model在将图像馈送到 model 之前裁剪图像

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)

this will speed up the inference time as well as there is less data to process by the model这将加快推理时间,并且 model 需要处理的数据更少

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何让语音识别理解我选择的特定单词? Python - How do i make speech recognition understand spesific words i choose ? Python 我如何在乌龟中进行颜色检测 - How do i make colour detection in turtle 如何创建一个仅跟踪 python 中具有偶数区域的矩形的变量 - How do I make a variable that only keeps track of rectangles that have an even area in python 如何制作带有侧面板和内容区域的可调整大小的窗口? - How do I make a resizeable window with a sidepanel and content area? Plotly:如何在没有填充区域的情况下制作等高线图? - Plotly: How do I make a contour plot without the area filled? 如何使用Python更改当前游戏区域的引擎? - How do I make an engine to change the current area of a game in Python? 如何使用flow_from_directory仅选择特定的文件格式? - How to choose only spesific file format with flow_from_directory? 如何使用 webhook 使用 python 和 dialogflow 制作简单的面积计算器? - how do i make simple area calculator with python and dialogflow using webhook? 如何确定希尔伯特曲线始终填充相同区域的空间? - how do i make sure my hilbert curve always fill a space of the same area? 如何格式化 matplotlib 堆叠面积图轴以仅显示给定范围? - How do I format matplotlib stacked area chart axis to show only range given?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM