如何将Meteor链接到Opencv python

2024-04-25 15:16:24 发布

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

网页中有一个按钮负责激活python脚本。我使用这个包Python-Shell来运行这个命令。但我有这个错误,当按钮被点击时。我可以看到网络摄像头的灯打开然后关闭。此外,该脚本可以在cmd中手动执行。有什么想法吗

错误消息

W20200409-19:24:18.229(8)? (STDERR) meteor://💻app/app/app.js:283
W20200409-19:24:18.271(8)? (STDERR)       if (err) throw err;
W20200409-19:24:18.272(8)? (STDERR)                ^
W20200409-19:24:18.273(8)? (STDERR)
W20200409-19:24:18.275(8)? (STDERR) PythonShellError: cv2.error: OpenCV(4.1.2) C:\projects\opencv-python\opencv\modules\objdetect\src\cascadedetect.cpp:1689: error: (-215:Assertion failed) !empty() in function 'cv::CascadeClassifier::detectMultiScale'
W20200409-19:24:18.276(8)? (STDERR)     at PythonShell.parseError (C:\Users\Yue Qi Dong\Documents\GitHub\attendanceSystem\node_modules\python-shell\index.js:260:21)
W20200409-19:24:18.277(8)? (STDERR)     at terminateIfNeeded (C:\Users\Yue Qi Dong\Documents\GitHub\attendanceSystem\node_modules\python-shell\index.js:139:32)
W20200409-19:24:18.277(8)? (STDERR)     at ChildProcess.<anonymous> (C:\Users\Yue Qi Dong\Documents\GitHub\attendanceSystem\node_modules\python-shell\index.js:131:13)
W20200409-19:24:18.278(8)? (STDERR)     at ChildProcess.emit (events.js:311:20)
W20200409-19:24:18.279(8)? (STDERR)     at ChildProcess.EventEmitter.emit (domain.js:482:12)
W20200409-19:24:18.279(8)? (STDERR)     at Process.ChildProcess._handle.onexit (internal/child_process.js:275:12)
W20200409-19:24:18.282(8)? (STDERR)     ----- Python Traceback -----
W20200409-19:24:18.282(8)? (STDERR)     File "C:\Users\Yue Qi Dong\face\detect_face.py", line 18, in <module>
W20200409-19:24:18.283(8)? (STDERR)       faces = face_cascade.detectMultiScale(gray, 1.1, 4) {
W20200409-19:24:18.284(8)? (STDERR)   traceback: 'Traceback (most recent call last):\r\n' +
W20200409-19:24:18.284(8)? (STDERR)     '  File "C:\\Users\\Yue Qi Dong\\face\\detect_face.py", line 18, in <module>\r\n' +
W20200409-19:24:18.285(8)? (STDERR)     '    faces = face_cascade.detectMultiScale(gray, 1.1, 4)\r\n' +
W20200409-19:24:18.285(8)? (STDERR)     "cv2.error: OpenCV(4.1.2) C:\\projects\\opencv-python\\opencv\\modules\\objdetect\\src\\cascadedetect.cpp:1689: error: (-215:Assertion failed) !empty() in function 'cv::CascadeClassifier::detectMultiScale'\r\n" +
W20200409-19:24:18.286(8)? (STDERR)     '\r\n',
W20200409-19:24:18.286(8)? (STDERR)   executable: 'py',
W20200409-19:24:18.287(8)? (STDERR)   options: null,
W20200409-19:24:18.287(8)? (STDERR)   script: 'C:\\Users\\Yue Qi Dong\\face\\detect_face.py',
W20200409-19:24:18.287(8)? (STDERR)   args: null,
W20200409-19:24:18.288(8)? (STDERR)   exitCode: 1
W20200409-19:24:18.288(8)? (STDERR) }

流星法

Meteor.methods({
  myPythonCall() {
    PythonShell.run('C:/Users/Yue Qi Dong/face/detect_face.py', null, function (err) {
      if (err) throw err;
      console.log('finished');
});
   }
});

检测_face.py

import cv2

flag = 1 
num = 1 

# Load the cascade
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')

    # To capture video from webcam.
cap = cv2.VideoCapture(0, cv2.CAP_DSHOW)

while True:
    # Read the frame
    _, img = cap.read()
    # Convert to grayscale
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    # Detect the faces
    faces = face_cascade.detectMultiScale(gray, 1.1, 4)
    # Draw the rectangle around each face
    for (x, y, w, h) in faces:
        cv2.rectangle(img, (x, y), (x+w, y+h), (0, 255, 0), 2)
        sub_face = img[y:y+h, x:x+w]
    # Display
    img_flip=cv2.flip(img,1)
    cv2.imshow('img', img_flip)
    # Stop if escape key is pressed
    k = cv2.waitKey(30) & 0xff
    if k == ord('s'):  
        cv2.imwrite("C:/Windows/System32/face/"+ str(num) + ".jpg", sub_face)
        print(cap.get(3)); 
        print(cap.get(4));
        print("success to save"+str(num)+".jpg")
        print("-------------------------")
        num += 1

    elif k==27:
        break

# Release the VideoCapture object
cap.release()
cv2.destroyAllWindows()

如果有人能给我一些建议,我将不胜感激


Tags: inpymodulesimgstderrjscv2users
1条回答
网友
1楼 · 发布于 2024-04-25 15:16:24

我是一个Python新手,所以请容忍我

import cv2

flag = 1
num = 1

# Load the cascade
face_cascade = cv2.CascadeClassifier('/home/harry/Projects/meteor-app/haarcascade_frontalface_default.xml')

# To capture video from webcam.
cap = cv2.VideoCapture(0)
...

首先,我从Github下载了haarcascade_frontalface_default.xml,并将其与python脚本放在同一级别上,因为出于某种原因,该脚本出现了无法找到它的错误

第二,正如您所看到的,我正在指定文件的完整绝对路径,因为如果要记录python文件运行的路径,它将是/home/harry/Projects/meteor-app/.meteor/local/build/programs/server,因此python脚本将无法在同一级别上定位.xml文件

第三,省略给cv2.VideoCapture(0)的第二个值

一旦我对python脚本做了这些更改,Meteor应用程序就能够顺利运行。祝你好运

相关问题 更多 >