我希望我的 Opencv 使用线程来提高 fps

发布于 2025-01-16 23:47:09 字数 7660 浏览 2 评论 0原文

我的 opencv 代码在 raspbpi 中滞后,但在 pc 中很流畅。任何人都可以帮助我将我的硬代码转换为使用线程的代码。


from cv2 import cv2
import numpy as np
from pyzbar.pyzbar import decode
import pickle,time
import os
import imutils
import screeninfo
from screeninfo import get_monitors





curr_path = os.getcwd()
#########models##################################################
print("Loading face detection model")
proto_path = os.path.join(curr_path, 'model', 'deploy.prototxt')
model_path = os.path.join(curr_path, 'model', 'res10_300x300_ssd_iter_140000.caffemodel')
face_detector = cv2.dnn.readNetFromCaffe(prototxt=proto_path, caffeModel=model_path)
print("Loading face recognition model")
recognition_model = os.path.join(curr_path, 'model', 'openface_nn4.small2.v1.t7')
face_recognizer = cv2.dnn.readNetFromTorch(model=recognition_model)
################pickles#########################################
recognizer = pickle.loads(open('recognizer.pickle', "rb").read())
le = pickle.loads(open('le.pickle', "rb").read())

print("Starting test video file")
#adjacents########################################################################
no_of_adjacent_prediction=0
no_face_detected=0  #to track the number of times the face is detected
prev_predicted_name=''   #to keep track of the previously predicted face(w.r.t frame)
count_frames = total_no_face_detected = 0
#camera#########################################################################
font=cv2.FONT_HERSHEY_SIMPLEX
clr=(255,255,255)
cap = cv2.VideoCapture(0)
time.sleep(2)

profile = None

####TRY_COUNTS###########
MAX_TRY=3
tries=0  #
######flags############
flag = True
flag_face_recognised=False   #to keep track if the user face is recognized
flag_face_not_recognised=False
#############FULLSCREEN###############
WINDOW_NAME = "Face-Rcognition and QRCODEQQQQQ"
screenid = 0
while True:
    cv2.namedWindow(WINDOW_NAME, cv2.WND_PROP_FULLSCREEN)
    cv2.setWindowProperty(WINDOW_NAME, cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)
    ret, frame = cap.read()
    
    screen = screeninfo.get_monitors()[screenid]
    screen_width, screen_height = screen.width,screen.height

    frame = cv2.flip(frame, 1)
    frame_height, frame_width, _ = frame.shape

    scaleWidth = float(screen_width) / float(frame_width)
    scaleHeight = float(screen_height) / float(frame_height)

    if (flag):
        access = open("AccessCodes.txt")
        for i in decode(frame):
            decoded_data = i.data.decode("utf-8")  # converts bytes to string value
            print(decoded_data)

            # Drawing polygon on frame (tilts w.r.t orientation)
            pts = np.array([i.polygon], np.int32)

            pts = pts.reshape((-1, 1, 2))
            cv2.polylines(frame, [pts], True, (0, 255, 0), 1)
            # print(pts)

            # Display text
            rect_pts = i.rect  # using rect point as origin for text as we don't want the text to tilt with the qrcode
            fontScale = 0.8
            thickness = 1
            # cv2.putText(frame,decoded_data,(rect_pts[0],rect_pts[1]),cv2.FONT_HERSHEY_SIMPLEX,fontScale,(255,0,0),thickness)
            # print(rect_pts)

            if decoded_data.lower() in access.read():  # Check private key
                flag = False
                tries = 0
                print("QRCODE is Valid.Proceed to FaceRecog")
                time_out_no_of_frames_after_qrcode = 0

            else:
                # print("INVALID QR CODE")
                print("Invalid QRCODE")
        if scaleHeight > scaleWidth:
            imgScale = scaleWidth

        else:
            imgScale = scaleHeight
            newX, newY = frame.shape[1] * imgScale, frame.shape[0] * imgScale
            frame = cv2.resize(frame, (int(newX), int(newY)))
            cv2.imshow(WINDOW_NAME, frame)
    else:
        frame = cv2.resize(frame, (int(newX), int(newY)))

        (h, w) = frame.shape[:2]

        image_blob = cv2.dnn.blobFromImage(cv2.resize(frame, (300, 300)), 1.0, (300, 300), (104.0, 177.0, 123.0), False, False)

        face_detector.setInput(image_blob)
        face_detections = face_detector.forward()

        for i in range(0, face_detections.shape[2]):
            confidence = face_detections[0, 0, i, 2]
            if confidence > 0.90:
                box = face_detections[0, 0, i, 3:7] * np.array([w, h, w, h])
                (startX, startY, endX, endY) = box.astype("int")
                face = frame[startY:endY, startX:endX]
                (fH, fW) = face.shape[:2]
                face_blob = cv2.dnn.blobFromImage(face, 1.0/255, (96, 96), (0, 0, 0), True, False)

                face_recognizer.setInput(face_blob)
                vec = face_recognizer.forward()

                preds = recognizer.predict_proba(vec)[0]
                j = np.argmax(preds)
                proba = preds[j]
                name = le.classes_[j]

                text = "{}: {:.2f}".format(name, proba )
                y = startY - 10 if startY - 10 > 10 else startY + 10
                cv2.rectangle(frame, (startX, startY), (endX, endY), (0, 0, 255), 2)
                cv2.putText(frame, text, (startX, y), cv2.FONT_HERSHEY_SIMPLEX, 0.65, (0, 0, 255), 2)

                cv2.putText(frame, "Welcome home " + name.replace('_', ' ').title(), (160, 460), font, 0.8, clr,
                            thickness+3, cv2.LINE_AA)
                cv2.rectangle(frame, (startX, startY), (endX, endY), (255, 255, 255), 1)
                if name == decoded_data.lower():
                    print("Face is Recognised: "+str(no_of_adjacent_prediction))
                    no_of_adjacent_prediction += 1
                else:
                    print("Face not Recognised.")
                    cv2.putText(frame, "Face Not Recognised", (160, 460), font, 0.8, clr, thickness, cv2.LINE_AA)
                    flag_face_not_recognised = True
                    no_of_adjacent_prediction = 0
                if (no_of_adjacent_prediction > 10):  # no_of_adjacent_prediction is only updated when the confidence of classification is >80

                    flag_face_recognised = True
                    no_of_adjacent_prediction = 0
                    no_face_detected = 0
        cv2.imshow(WINDOW_NAME, frame)
        if (flag_face_recognised):  # if face is recognized then open the door
            # arduino.write(bytes('o', 'utf-8'))  #Output the given byte string over the serial port.
            print("DOOR is OPEN")
            time.sleep(5)
            # speak("Closing door")
            # arduino.write(bytes('c', 'utf-8'))  #Output the given byte string over the serial port.
            print("DOOR is CLOSED")
            flag_face_recognised = False
            flag = True  # to start from qrcode

        if (flag_face_not_recognised):
            # speak("Face not recognised. The door will remain closed")
            time.sleep(2)
            flag_face_not_recognised = False
            tries += 1
            if (tries >= MAX_TRY):
                flag = True  # to start from qrcode
                tries = 0

        if (time_out_no_of_frames_after_qrcode >= 400):
            # speak("User authentication failed due to time out")
            flag = True  # to start from qrcode


    key = cv2.waitKey(1) & 0xFF

    if key == ord('q'):
        break

cv2.destroyAllWindows()


<块引用> <块引用> <块引用> <块引用> <块引用>

FPS 电脑:20 fps FPS 树莓派:9 fps

我尝试了各种 opencv 代码,结果仍然相同。我找到了一个解决方案,线程增加了 opencv 的 fps,但我不知道如何将其应用到我的代码中,因为我是一个 python 新手。如果有帮助就好了。我希望树莓派中的 fps 范围为 15-20,而不是 9 fps。

My opencv code is lagging in raspbpi but in pc its smooth. Can anyone help me make my hard code to a code that uses threading.


from cv2 import cv2
import numpy as np
from pyzbar.pyzbar import decode
import pickle,time
import os
import imutils
import screeninfo
from screeninfo import get_monitors





curr_path = os.getcwd()
#########models##################################################
print("Loading face detection model")
proto_path = os.path.join(curr_path, 'model', 'deploy.prototxt')
model_path = os.path.join(curr_path, 'model', 'res10_300x300_ssd_iter_140000.caffemodel')
face_detector = cv2.dnn.readNetFromCaffe(prototxt=proto_path, caffeModel=model_path)
print("Loading face recognition model")
recognition_model = os.path.join(curr_path, 'model', 'openface_nn4.small2.v1.t7')
face_recognizer = cv2.dnn.readNetFromTorch(model=recognition_model)
################pickles#########################################
recognizer = pickle.loads(open('recognizer.pickle', "rb").read())
le = pickle.loads(open('le.pickle', "rb").read())

print("Starting test video file")
#adjacents########################################################################
no_of_adjacent_prediction=0
no_face_detected=0  #to track the number of times the face is detected
prev_predicted_name=''   #to keep track of the previously predicted face(w.r.t frame)
count_frames = total_no_face_detected = 0
#camera#########################################################################
font=cv2.FONT_HERSHEY_SIMPLEX
clr=(255,255,255)
cap = cv2.VideoCapture(0)
time.sleep(2)

profile = None

####TRY_COUNTS###########
MAX_TRY=3
tries=0  #
######flags############
flag = True
flag_face_recognised=False   #to keep track if the user face is recognized
flag_face_not_recognised=False
#############FULLSCREEN###############
WINDOW_NAME = "Face-Rcognition and QRCODEQQQQQ"
screenid = 0
while True:
    cv2.namedWindow(WINDOW_NAME, cv2.WND_PROP_FULLSCREEN)
    cv2.setWindowProperty(WINDOW_NAME, cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)
    ret, frame = cap.read()
    
    screen = screeninfo.get_monitors()[screenid]
    screen_width, screen_height = screen.width,screen.height

    frame = cv2.flip(frame, 1)
    frame_height, frame_width, _ = frame.shape

    scaleWidth = float(screen_width) / float(frame_width)
    scaleHeight = float(screen_height) / float(frame_height)

    if (flag):
        access = open("AccessCodes.txt")
        for i in decode(frame):
            decoded_data = i.data.decode("utf-8")  # converts bytes to string value
            print(decoded_data)

            # Drawing polygon on frame (tilts w.r.t orientation)
            pts = np.array([i.polygon], np.int32)

            pts = pts.reshape((-1, 1, 2))
            cv2.polylines(frame, [pts], True, (0, 255, 0), 1)
            # print(pts)

            # Display text
            rect_pts = i.rect  # using rect point as origin for text as we don't want the text to tilt with the qrcode
            fontScale = 0.8
            thickness = 1
            # cv2.putText(frame,decoded_data,(rect_pts[0],rect_pts[1]),cv2.FONT_HERSHEY_SIMPLEX,fontScale,(255,0,0),thickness)
            # print(rect_pts)

            if decoded_data.lower() in access.read():  # Check private key
                flag = False
                tries = 0
                print("QRCODE is Valid.Proceed to FaceRecog")
                time_out_no_of_frames_after_qrcode = 0

            else:
                # print("INVALID QR CODE")
                print("Invalid QRCODE")
        if scaleHeight > scaleWidth:
            imgScale = scaleWidth

        else:
            imgScale = scaleHeight
            newX, newY = frame.shape[1] * imgScale, frame.shape[0] * imgScale
            frame = cv2.resize(frame, (int(newX), int(newY)))
            cv2.imshow(WINDOW_NAME, frame)
    else:
        frame = cv2.resize(frame, (int(newX), int(newY)))

        (h, w) = frame.shape[:2]

        image_blob = cv2.dnn.blobFromImage(cv2.resize(frame, (300, 300)), 1.0, (300, 300), (104.0, 177.0, 123.0), False, False)

        face_detector.setInput(image_blob)
        face_detections = face_detector.forward()

        for i in range(0, face_detections.shape[2]):
            confidence = face_detections[0, 0, i, 2]
            if confidence > 0.90:
                box = face_detections[0, 0, i, 3:7] * np.array([w, h, w, h])
                (startX, startY, endX, endY) = box.astype("int")
                face = frame[startY:endY, startX:endX]
                (fH, fW) = face.shape[:2]
                face_blob = cv2.dnn.blobFromImage(face, 1.0/255, (96, 96), (0, 0, 0), True, False)

                face_recognizer.setInput(face_blob)
                vec = face_recognizer.forward()

                preds = recognizer.predict_proba(vec)[0]
                j = np.argmax(preds)
                proba = preds[j]
                name = le.classes_[j]

                text = "{}: {:.2f}".format(name, proba )
                y = startY - 10 if startY - 10 > 10 else startY + 10
                cv2.rectangle(frame, (startX, startY), (endX, endY), (0, 0, 255), 2)
                cv2.putText(frame, text, (startX, y), cv2.FONT_HERSHEY_SIMPLEX, 0.65, (0, 0, 255), 2)

                cv2.putText(frame, "Welcome home " + name.replace('_', ' ').title(), (160, 460), font, 0.8, clr,
                            thickness+3, cv2.LINE_AA)
                cv2.rectangle(frame, (startX, startY), (endX, endY), (255, 255, 255), 1)
                if name == decoded_data.lower():
                    print("Face is Recognised: "+str(no_of_adjacent_prediction))
                    no_of_adjacent_prediction += 1
                else:
                    print("Face not Recognised.")
                    cv2.putText(frame, "Face Not Recognised", (160, 460), font, 0.8, clr, thickness, cv2.LINE_AA)
                    flag_face_not_recognised = True
                    no_of_adjacent_prediction = 0
                if (no_of_adjacent_prediction > 10):  # no_of_adjacent_prediction is only updated when the confidence of classification is >80

                    flag_face_recognised = True
                    no_of_adjacent_prediction = 0
                    no_face_detected = 0
        cv2.imshow(WINDOW_NAME, frame)
        if (flag_face_recognised):  # if face is recognized then open the door
            # arduino.write(bytes('o', 'utf-8'))  #Output the given byte string over the serial port.
            print("DOOR is OPEN")
            time.sleep(5)
            # speak("Closing door")
            # arduino.write(bytes('c', 'utf-8'))  #Output the given byte string over the serial port.
            print("DOOR is CLOSED")
            flag_face_recognised = False
            flag = True  # to start from qrcode

        if (flag_face_not_recognised):
            # speak("Face not recognised. The door will remain closed")
            time.sleep(2)
            flag_face_not_recognised = False
            tries += 1
            if (tries >= MAX_TRY):
                flag = True  # to start from qrcode
                tries = 0

        if (time_out_no_of_frames_after_qrcode >= 400):
            # speak("User authentication failed due to time out")
            flag = True  # to start from qrcode


    key = cv2.waitKey(1) & 0xFF

    if key == ord('q'):
        break

cv2.destroyAllWindows()


FPS PC: 20 fps
FPS RASPBERRY PI : 9 fps

i tried various opencv codes and the result is still the same. I found a solution that threading increases the fps of opencv but i do not know how to apply this to my code due the fact that i am a noob python kid. a help would be nice. I want my fps in my raspberry ranging from 15-20 instead of 9 fps.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文