如何 link Meteor 到 Opencv python
How to link Meteor to Opencv python
网页中有一个按钮负责激活python脚本。而我用这个包 Python-Shell to 运行 命令。但是当单击按钮时,我遇到了这个错误。我可以看到网络摄像头的灯打开然后关闭。此外,脚本可以在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');
});
}
});
detect_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()
如果有人能给我一些想法,我将不胜感激。
我是 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 文件 运行ning 所在的路径,它将是/home/harry/Projects/meteor-app/.meteor/local/build/programs/server
因此 python 脚本将无法在同一级别找到 .xml
文件。
三、给cv2.VideoCapture(0)
的第二个值省略
一旦我对 python 脚本进行了这些更改,Meteor 应用程序就能够 运行 顺利进行。祝你好运!
网页中有一个按钮负责激活python脚本。而我用这个包 Python-Shell to 运行 命令。但是当单击按钮时,我遇到了这个错误。我可以看到网络摄像头的灯打开然后关闭。此外,脚本可以在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');
});
}
});
detect_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()
如果有人能给我一些想法,我将不胜感激。
我是 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 文件 运行ning 所在的路径,它将是/home/harry/Projects/meteor-app/.meteor/local/build/programs/server
因此 python 脚本将无法在同一级别找到 .xml
文件。
三、给cv2.VideoCapture(0)
的第二个值省略
一旦我对 python 脚本进行了这些更改,Meteor 应用程序就能够 运行 顺利进行。祝你好运!