python线程不工作。解决办法是什么?

2024-05-29 03:10:41 发布

您现在位置:Python中文网/ 问答频道 /正文

这部分代码将pysimple gui应用于yolo

我想试试这段代码中的线程

我想在“while”中运行一个线程,每2.5秒打印一个句子

但是,此函数不是每2.5秒运行一次,而是每运行一次when循环

while循环中每2.5秒运行一次的方法是什么

import PySimpleGUIQt as sg
##########thread test########################
def print_text():
print("yolo run")

threading.Timer(2.5, print_text).start()
################################################

螺纹试验方法

# loop over frames from the video file stream
win_started = False
if use_webcam:
    cap = cv2.VideoCapture(0)
while True:
    print_text()###########################################test thread

何时在while循环中声明线程函数

    # read the next frame from the file or webcam
    if use_webcam:
        grabbed, frame = cap.read()
    else:
        grabbed, frame = vs.read()

    # if the frame was not grabbed, then we have reached the end
    # of the stream
    if not grabbed:
        break

    # if the frame dimensions are empty, grab them
    if W is None or H is None:
        (H, W) = frame.shape[:2]

    # construct a blob from the input frame and then perform a forward
    # pass of the YOLO object detector, giving us our bounding boxes
    # and associated probabilities
    blob = cv2.dnn.blobFromImage(frame, 1 / 255.0, (416, 416),
        swapRB=True, crop=False)
    net.setInput(blob)
    start = time.time()
    layerOutputs = net.forward(ln)
    end = time.time()

    # initialize our lists of detected bounding boxes, confidences,
    # and class IDs, respectively
    boxes = []
    confidences = []
    classIDs = []

    
    # apply non-maxima suppression to suppress weak, overlapping
    # bounding boxes
    idxs = cv2.dnn.NMSBoxes(boxes, confidences, gui_confidence, gui_threshold)

    # ensure at least one detection exists
    if len(idxs) > 0:
        # loop over the indexes we are keeping
        for i in idxs.flatten():
            # extract the bounding box coordinates
            (x, y) = (boxes[i][0], boxes[i][1])
            (w, h) = (boxes[i][2], boxes[i][3])

            # draw a bounding box rectangle and label on the frame
            color = [int(c) for c in COLORS[classIDs[i]]]
            cv2.rectangle(frame, (x, y), (x + w, y + h), color, 2)
            text = "{}: {:.4f}".format(LABELS[classIDs[i]],
                confidences[i])
            cv2.putText(frame, text, (x, y - 5),
                cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
    



    imgbytes = cv2.imencode('.png', frame)[1].tobytes()  # ditto

    

Tags: andthetextiftimegui线程cv2
1条回答
网友
1楼 · 发布于 2024-05-29 03:10:41

也许您将while循环放在线程将要执行的代码中

import time
from threading import Thread

def print_text():
     while True:
         print('yolo run')
         time.sleep(2.5) 
# Thread waits 2.5 seconds and then return to the beginning of the while loop

为您的主代码

# creating thread
print_thread = threading.Thread(target=print_text(), args=(1,))
# start thread
print_thread.start()
# wait thread to finish
print_thread.join()

此代码将创建一个能够执行计算的单线程,直到满足某些停止条件

相关问题 更多 >

    热门问题