简体   繁体   English

后台运行任务/function

[英]Running task / function in the background

i wrote a program to capture the position of license plate with my webcam feed using YOLOv4.我编写了一个程序,使用 YOLOv4 使用我的网络摄像头提要捕获车牌的 position。 The result of the detection is then passed to easyOCR to do character identification.然后将检测结果传递给easyOCR进行字符识别。 Right now, im calling the OCR function in the while loop everytime a detection occured.现在,每次检测到时,我都会在 while 循环中调用 OCR function。 Is there a way to call the OCR function outside the loop without stopping the webcam feed?有没有办法在不停止网络摄像头馈送的情况下在循环外调用 OCR function? some people suggested me to use queue or sub process but im not quite familiar with the concept.有些人建议我使用队列或子进程,但我不太熟悉这个概念。 Any help would be very appreciated任何帮助将不胜感激

#detection
while 1:
    #_, pre_img = cap.read()
    #pre_img= cv2.resize(pre_img, (640, 480))
    _, img = cap.read()
    #img = cv2.flip(pre_img,1)
    hight, width, _ = img.shape
    blob = cv2.dnn.blobFromImage(img, 1 / 255, (416, 416), (0, 0, 0), swapRB=True, crop=False)

    net.setInput(blob)

    output_layers_name = net.getUnconnectedOutLayersNames()

    layerOutputs = net.forward(output_layers_name)

    boxes = []
    confidences = []
    class_ids = []

    for output in layerOutputs:
        for detection in output:
            score = detection[5:]
            class_id = np.argmax(score)
            confidence = score[class_id]
            if confidence > 0.7:
                center_x = int(detection[0] * width)
                center_y = int(detection[1] * hight)
                w = int(detection[2] * width)
                h = int(detection[3] * hight)
                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, .5, .4)

    boxes = []
    confidences = []
    class_ids = []

    for output in layerOutputs:
        for detection in output:
            score = detection[5:]
            class_id = np.argmax(score)
            confidence = score[class_id]
            if confidence > 0.5:
                center_x = int(detection[0] * width)
                center_y = int(detection[1] * hight)
                w = int(detection[2] * width)
                h = int(detection[3] * hight)

                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, .8, .4)
    font = cv2.FONT_HERSHEY_PLAIN
    colors = np.random.uniform(0, 255, size=(len(boxes), 3))
    if len(indexes) > 0:
        for i in indexes.flatten():
            x, y, w, h = boxes[i]
            label = str(classes[class_ids[i]])
            confidence = str(round(confidences[i], 2))
            color = colors[i]
            cv2.rectangle(img, (x, y), (x + w, y + h), color, 2)
            detected_image = img[y:y+h, x:x+w]
            cv2.putText(img, label + " " + confidence, (x, y + 400), font, 2, color, 2)
            #print(detected_image)
            cv2.imshow('detection',detected_image)

            result = OCR(detected_image)
            print(result)

Function for OCR Function 用于 OCR

def OCR(cropped_image):

    result = reader.readtext(cropped_image)
    text = ''
    for result in result:
        text += result[1] + ' '

    spliced = (remove(text)).upper()
    return spliced

You could run the OCR function on an other thread with the thread library like so:您可以使用线程库在另一个线程上运行 OCR function,如下所示:

import time  # not necessary only to simulate work time
import _thread as thread  # in python 3 the name has changed to _thread


def OCR(cropped_image):

    result = reader.readtext(cropped_image)
    text = ''
    for result in result:
        text += result[1] + ' '

    spliced = (remove(text)).upper()
    print(spliced) # you would have to print the result in the OCR function because you can't easily return stuff


while 1:
    time.sleep(5)  # simulating some work time
    print("main")

    detected_image = 1
    thread.start_new_thread(OCR, (detected_image,)) # calling the OCR function on a new thread.

I hope it will help you...我希望它会帮助你...

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM