I have been running python script with nodejs using python-shell package.. and i am getting this error:
Error: init done
at PythonShell.parseError (F:\github\pythonShellDemo\node_modules\python-shell\index.js:191:17)
at terminateIfNeeded (F:\github\pythonShellDemo\node_modules\python-shell\index.js:98:28)
at ChildProcess.<anonymous> (F:\github\pythonShellDemo\node_modules\python-shell\index.js:89:9)
at emitTwo (events.js:126:13)
at ChildProcess.emit (events.js:214:7)
at Process.ChildProcess._handle.onexit (internal/child_process.js:198:12)
after some debugging and research i got to know that this error is with opencv but i cant find any solution..
here is the code:
import cv2
import zbar
from PIL import Image
import sys
video = cv2.VideoCapture(0)
count=0
qrcode=[]
while True:
ret, frame = video.read()
cv2.imshow('Camera', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
grayscale = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
image = Image.fromarray(grayscale)
width, height = image.size
zbarimage = zbar.Image(width, height,'Y800', image.tobytes())
scanner = zbar.ImageScanner()
scanner.scan(zbarimage)
for x in zbarimage:
if count == 0:
qrcode=x.data
count=count+1
if qrcode:
break
video.release()
cv2.destroyAllWindows()
print(qrcode)
sys.stdout.flush()
I am using python 2.7
UPDATE:
nodejs code for calling python script:
PythonShell.run('python/scan.py', options, function (err, results) {
if (err) {
console.log(err)
reject(err)
}
// results is an array consisting of messages collected during execution
console.log(results)
resolve(results)
})
UPDATE:
i tried running opencv only without zbar.. still got the error
import cv2
cap = cv2.VideoCapture(0)
while(True):
ret, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('frame',gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
The OpenCV library prints init done to the standard error output. It's not an error, but just a debug print. python-shell then turns this into an error. From the python-shell documentation:
If the script writes to stderr or exits with a non-zero code, an error will be thrown.
Suppressing the output seems to be only possible by recompiling the library with a parameter set.
Related
My opencv installation recently stopped working for reasons I'm not sure of. I have two scripts that all give different errors:
Script A:
import cv2
cap = cv2.VideoCapture(0)
while(True):
ret, frame = cap.read()
rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2BGRA)
cv2.imshow('frame', rgb)
gives me this error:
select timeout
VIDIOC_DQBUF: Resource temporarily unavailable
Traceback (most recent call last):
File "camera.py", line 19, in <module>
rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2BGRA)
cv2.error: OpenCV(4.0.1-dev) /home/me/Packages/opencv/modules/imgproc/src/color.cpp:181: error: (-215:Assertion failed) !_src.empty() in function 'cvtColor'
import cv2
cap = cv2.VideoCapture(0)
while(True):
ret, frame = cap.read()
rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2BGRA)
cv2.imshow('frame', rgb)
And Script B:
import cv2
def show_webcam(mirror=False):
cam = cv2.VideoCapture(0)
while True:
ret_val, img = cam.read()
if mirror:
img = cv2.flip(img, 1)
cv2.imshow('my webcam', img)
if cv2.waitKey(1) == 27:
break # esc to quit
cv2.destroyAllWindows()
def main():
show_webcam(mirror=True)
gives me this error:
select timeout
VIDIOC_DQBUF: Resource temporarily unavailable
Traceback (most recent call last):
File "camera3.py", line 26, in <module>
main()
File "camera3.py", line 22, in main
show_webcam(mirror=True)
File "camera3.py", line 15, in show_webcam
cv2.imshow('my webcam', img)
cv2.error: OpenCV(4.0.1-dev) /home/david/Packages/opencv/modules/highgui/src/window.cpp:352: error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'imshow'
Here's the main issue: I've used OpenCV before and both of those errors usually occur when opencv can't find the webcam. But I do have a webcam attached, and when I open Cheese Webcam Booth it works fine, and takes pictures fine. Is there a way to repair this without reinstalling OpenCV?
I'm on Ubuntu 18.04.
I would review the OpenCV installation, since Script A works just fine by me, on the same version of the library and similar OS (Mint 19).
import cv2
cap = cv2.VideoCapture(0)
while(True):
ret, frame = cap.read()
rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2BGRA)
cv2.imshow('frame', rgb)
cv2.waitKey(10)
The installation through
pip install opencv-python
is known for having "problems" with 3rd party modules (https://github.com/opencv/opencv/issues/8471).
Try building and installing from source, this way the interface modules will be built and linked as well (V4L, FFMpeg, etc).
Two possible solutions: 1) set the correct fps; 2) upgrade to the newer version of opencv for python.
in both cases, the camera was activated but is now occupied. So now, in the main "while" loop try putting
while(True):
ret_val, img = cap.read()
if(ret_val==False)
cap.open(0)
continue
...
I am using Ubuntu 16.04 on vm player. I have written a script to connect laptop's webcam. But, I got select timeout error with ret, frame = video_capture.read(), although I selected camera from "removable devices" section.
When I tried to run script on windows, it was successful.
code: read_video.py
import numpy as np
import cv2
video_capture = cv2.VideoCapture(0)
while(True):
ret, frame = video_capture.read()
cv2.imshow("Frame",frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
video_capture.release()
cv2.destroyAllWindows()
error:
read_video.py
select timeout
select timeout
OpenCV Error: Assertion failed (total() == 0 || data != __null) in Mat, file /tmp/binarydeb/ros-kinetic-opencv3-3.3.1/modules/core/include/opencv2/core/mat.inl.hpp, line 500
Traceback (most recent call last):
File "read_video.py", line 9, in <module>
ret, frame = video_capture.read()
cv2.error: /tmp/binarydeb/ros-kinetic-opencv3-3.3.1/modules/core/include/opencv2/core/mat.inl.hpp:500: error: (-215) total() == 0 || data != __null in function Mat
How can I solve it?
I want to reduce the resolution of video that I am getting from my webcam to half(i.e from 640x480 to 320x240) but I am getting error.
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
cap.set(3,320)
cap.set(4,240)
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
# Our operations on the frame come here
#gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Display the resulting frame
cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
The error that I am getting is
Traceback (most recent call last): File
"C:\Users\Nemi\Desktop\bbb.py", line 17, in
cv2.imshow('frame',frame) error: ......\opencv-2.4.13.2\modules\highgui\src\window.cpp:269: error:
(-215) size.width>0 && size.height>0 in function cv::imshow
I am new to this and could not find the solution. Could someone please tell me what am I doing wrong?
I am running the following code on Raspberry Pi with pi camera, I have the broadcom drivers for it and all, but I am getting an error. Perhaps something to do with the dimensions of the video feed, but I do not know how to set it on Linux.
Code:
import cv2
import numpy as np
cap = cv2.VideoCapture()
while True:
ret, img = cap.read()
cv2.imshow('img', img)
if cv2.waitKey(0) & 0xFF == ord('q):
break
Error:
OpenCV Error: Assertion failed (size.width>0 && size.height>0) in imshow,
file /home/pi/opencv-3.3.0/modules/highgui/src/window.cpp, line 325
Traceback (most recent call last):
File "check_picam_with_opencv.py", line 10, in <module>
cv2.imshow('img', img)
cv2.error: /home/pi/opencv-3.3.0/modules/highgui/src/window.cpp:325: error:
(-215) size.width>0 && size.height>0 in function imshow
Provide an id to VideoCapture.
cap = cv2.VideoCapture(0)
Also check the value of ret, see if it's TRUE or FALSE
print (ret)
Edit:
To capture a video, you need to create a VideoCapture object. Its argument can be either the device index or the name of a video file. Device index is just the number to specify which camera.
cap = cv2.VideoCapture(0)
To check whether the cap has been initialized or not, you can use cap.isOpened() function, which returns True for successful initialization and False for failure.
if cap.isOpened() == False:
print ("VideoCapture failed")
cap.read() returns a bool (True/False). If frame is read correctly, it will be True. So you can check end of the video by checking this return value.
ret, frame = cap.read()
if ret == False:
print("Frame is empty")
Further reading here.
I want to capture images from webcam and then further do image processing for ANPR (Automatic number plate Recognition) in python 2.7 using opencv 2.4.10 in Ubuntu 14.04. When I run this simple code, it detects my camera once and then camera stops working.
Code is:
import cv2
cam = cv2.VideoCapture(0)
s, img = cam.read()
winName = "Movement Indicator"
cv2.namedWindow(winName, cv2.CV_WINDOW_AUTOSIZE)
while s:
cv2.imshow( winName,img )
s, img = cam.read()
key = cv2.waitKey(10) & 0xFF
if key == 27:
cv2.destroyWindow(winName)
break
print "Goodbye"
Can someone please help me with this?
Got the answer. I was not releasing cam. It works fine now