多线程从函数到另一个函数,然后返回主函数

发布于 2025-01-28 23:58:05 字数 1696 浏览 6 评论 0原文

目前,这是我的设置,Pycharm的Python,还有带有Max30100传感器的Arduino Uno,以心率。目前,我能够通过pyserial从Arduino传输数据到我的Pycharm中,并将在Pycharm中显示Heartrate。 但是我希望我的程序运行网络摄像头fef ,而 同时显示传感器读数。 目前,我的 简化 代码如下:

class start_workout_session():

def pushup(self):
   
    #cam start here ~~!!
    cap = cv2.VideoCapture(0)

    with mp_pose.Pose(min_detection_confidence=1.5, min_tracking_confidence=1.5) as pose:

        while cap.isOpened():
           
            # Extract landmarks
            try:
             
               calculate angle here

               Showing angle in frame by using putText method from cv2
               
               Curl counter logic

               Render curl counter

                t1 = threading.Thread(target=arduino)
                t1.start()
    
               cv2.imshow('i-Trainer', image)

        cap.release()
        cv2.destroyAllWindows()



def arduino():
serialInst = serial.Serial()
serialInst.baudrate = 9600
serialInst.port = 'COM3'
serialInst.open()
while True:
    if serialInst.inWaiting:
        packet = serialInst.readline()
        print(packet.decode('utf').rstrip('\n'))

time.sleep(1)

t2 = threading.Thread(target=pushup)
t2.start()
t1.join()
t2.join()

该程序可以运行,但始终提示一个错误:

File "C:\Users\..\main.py", line 629, in arduino
    serialInst.open()



File "C:\Users\..\Python36\lib\site-packages\serial\serialwin32.py", line 64, in open
    raise SerialException("could not open port {!r}: {!r}".format(self.portstr, ctypes.WinError()))
serial.serialutil.SerialException: could not open port 'COM3': PermissionError(13, 'Access is denied.', None, 5)

currently this is my setup, python in PyCharm, and an Arduino Uno with MAX30100 sensor for heart rate. I am currently able to transmit data via pyserial into my pycharm from arduino, and will show the heartrate in pycharm.
But I want my program to run the webcam feed WHILE showing the sensor reading at the same time.
Currently my SIMPLIFIED code is as below :

class start_workout_session():

def pushup(self):
   
    #cam start here ~~!!
    cap = cv2.VideoCapture(0)

    with mp_pose.Pose(min_detection_confidence=1.5, min_tracking_confidence=1.5) as pose:

        while cap.isOpened():
           
            # Extract landmarks
            try:
             
               calculate angle here

               Showing angle in frame by using putText method from cv2
               
               Curl counter logic

               Render curl counter

                t1 = threading.Thread(target=arduino)
                t1.start()
    
               cv2.imshow('i-Trainer', image)

        cap.release()
        cv2.destroyAllWindows()



def arduino():
serialInst = serial.Serial()
serialInst.baudrate = 9600
serialInst.port = 'COM3'
serialInst.open()
while True:
    if serialInst.inWaiting:
        packet = serialInst.readline()
        print(packet.decode('utf').rstrip('\n'))

time.sleep(1)

t2 = threading.Thread(target=pushup)
t2.start()
t1.join()
t2.join()

this program can run but it always prompts an error :

File "C:\Users\..\main.py", line 629, in arduino
    serialInst.open()



File "C:\Users\..\Python36\lib\site-packages\serial\serialwin32.py", line 64, in open
    raise SerialException("could not open port {!r}: {!r}".format(self.portstr, ctypes.WinError()))
serial.serialutil.SerialException: could not open port 'COM3': PermissionError(13, 'Access is denied.', None, 5)

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦开始←不甜 2025-02-04 23:58:05

多亏了 @cguk70,

我设法解决了问题,将

 t1 = threading.Thread(target=arduino)
                t1.start()

外部放在一起的

#cam start here ~~!!
    cap = cv2.VideoCapture(0)

原因是我在相机循环中创建了一个新的Arduino线程。因此,第二次回合,您将尝试再次打开串行端口并获得错误,因为它已经打开。

Thanks to @cguk70,

i managed to solve the problem, by putting the

 t1 = threading.Thread(target=arduino)
                t1.start()

outside together with

#cam start here ~~!!
    cap = cv2.VideoCapture(0)

reason is that i am creating a new Arduino thread inside the camera loop. So the second time round you'll try and open the serial port again and get the error since it's already open.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文