代码之家  ›  专栏  ›  技术社区  ›  samuelkaris

如何将实时计算机视觉代码集成到django中

  •  0
  • samuelkaris  · 技术社区  · 1 年前

    我有这个使用MediaPipe和深度学习模型进行实时动作检测的代码。我想创建一个Django视频流网络应用程序,并集成此代码来检测帧中的动作。我尝试将整个代码(以及代码中调用的每个函数的函数定义)放在一个单独的python脚本中( camera.py ). 然后,我将整个类导入到我的 views.py 但是当我运行服务器时,这些帧不会显示。这是我想集成到Django应用程序中的代码:

    sequence = []
    sentence = []
    predictions = []
    threshold = 0.5
    
    cap = cv2.VideoCapture(0)
    # Set mediapipe model 
    with mp_holistic.Holistic(min_detection_confidence=0.5, min_tracking_confidence=0.5) as holistic:
        while cap.isOpened():
    
            # Read feed
            ret, frame = cap.read()
    
            # Make detections
            image, results = mediapipe_detection(frame, holistic)
            print(results)
            
            # Draw landmarks
            draw_styled_landmarks(image, results)
            
            # 2. Prediction logic
            keypoints = extract_keypoints(results)
            sequence.append(keypoints)
            sequence = sequence[-30:]
            
            if len(sequence) == 30:
                res = model.predict(np.expand_dims(sequence, axis=0))[0]
                print(actions[np.argmax(res)])
                predictions.append(np.argmax(res))
                
                
            #3. Viz logic
                if np.unique(predictions[-10:])[0]==np.argmax(res): 
                    if res[np.argmax(res)] > threshold: 
                        
                        if len(sentence) > 0: 
                            if actions[np.argmax(res)] != sentence[-1]:
                                sentence.append(actions[np.argmax(res)])
                        else:
                            sentence.append(actions[np.argmax(res)])
    
                if len(sentence) > 5: 
                    sentence = sentence[-5:]
    
                # Viz probabilities
                image = prob_viz(res, actions, image, colors)
                
            cv2.rectangle(image, (0,0), (640, 40), (245, 117, 16), -1)
            cv2.putText(image, ' '.join(sentence), (3,30), 
                           cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2, cv2.LINE_AA)
            
            # Show to screen
            cv2.imshow('OpenCV Feed', image)
    
            # Break gracefully
            if cv2.waitKey(10) & 0xFF == ord('q'):
                break
        cap.release()
        cv2.destroyAllWindows()
    

    我想知道里面会有什么 camera.py , views.py , urls.py 以及html模板。非常感谢!

    0 回复  |  直到 1 年前
        1
  •  0
  •   Tea Tech    1 年前

    你的camera.py不适用于Django网站。在当前代码中,您直接从运行脚本的设备获取视频。因此,如果你在客户端-服务器环境中运行camera.py,它会在服务器上搜索相机,但你想从用户那里捕获流。

    因此,为了实现您想要的,您需要设置与用户的套接字连接,并不断从那里捕获数据并将其发送到您的服务器,或者只使用WebRTC进行视频捕获。

    这是一个有点复杂的过程,我无法在代码中完全演示,请搜索这些主题,它应该能给你一些见解。