I'm just following the second example from this tutorial and using a different video. Can anyone help solve the problem of why it crashes? I thought it would be my cheap Windows 7 running on 2 GB RAM, but it's not even using up the RAM when it crashes.
import numpy as np
import cv2
cap = cv2.VideoCapture("F:\\OpenCV\\Cam1_Indoor.avi")
fgbg = cv2.createBackgroundSubtractorMOG2()
while(1):
ret, frame = cap.read()
fgmask = fgbg.apply(frame)
cv2.imshow('frame',fgmask)
k = cv2.waitKey(30) & 0xff
if k == 27:
break
cap.release()
cv2.destroyAllWindows()
UPDATE 1:
I still can't get this working, but I have tried to debug it some more. I found that regardless of the video file name I give it (even if it's totally bogus), the same crash happens.
UPDATE 2:
I found that the code below works on images, so my problem may have to do with video capture.
im_in = cv2.imread("F:\\OpenCV\\nickel.jpg", cv2.IMREAD_GRAYSCALE);
fgbg = cv2.createBackgroundSubtractorMOG2()
fgmask = fgbg.apply(im_in)
cv2.imshow('frame',fgmask)
cv2.waitKey(0)
Add this below import statements
cv2.ocl.setUseOpenCL(False)
Related
This is my first time using OpenCV and I'm having some difficulties. I just made a simple program to see if it was imported correctly:
import cv2
import numpy as np
print("hi")
cap = cv2.VideoCapture(0)
print(cap.isOpened())
print("hi")
while True:
ret, frame = cap.read()
cv2.imshow('frame', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
When I run this file on the MacOS terminal, it works fine and opens up the video window, but when I run this file with IDLE nothing happens. No errors or warnings, the first "hi" prints but nothing else and the program ends. It's like as soon as I try to use cv2 directly after the first "hi", the program just ends. I believe I'm using the same version of python in both my terminal and IDLE, and I've tried uninstalling and reinstalling OpenCV using pip. Did I not install OpenCV correctly to be using with IDLE? Any help is greatly appreciated!
Edit: Not sure if this is causing any problems, but when I first ran it from IDLE, it had a few "Python quit unexpectedly" windows before I tried it in terminal. When I tried it there, it asked for access to my computer's webcam. Since then, the "Python quit unexpectedly" windows haven't showed up when I tried IDLE, so maybe it just needs access to my camera? I don't see an option for that in my Mac settings though, there's no IDLE app option to give access to.
Can you try this.
Snippet:
# -*- coding: utf-8 -*-
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
print(cap.isOpened())
while(cap.isOpened()):
ret, frame = cap.read()
cv2.imshow('frame', frame)
if (cv2.waitKey(1) & 0xFF == ord('q')):
break
cap.release()
cv2.destroyAllWindows()
Or:
snippet:
import cv2
cv2.namedWindow("preview")
vc = cv2.VideoCapture(0)
rval, frame = vc.read()
while True:
if frame is not None:
cv2.imshow("preview", frame)
rval, frame = vc.read()
if cv2.waitKey(1) & 0xFF == ord('q'):
break
I would like to lunch a usb-camera at my raspberry pi, but when I try to start python code (opencv), it shows this message and stop a lunch
ASSERT: "false" in file qasciikey.cpp, line 501
Aborted
Can someone please explain me, what error is this?
It doesnt work even with another codes as well... But camera works fine, when I open it in programs like Camorama webcam viewer. I read that this part makes a problem
cv2.waitKey(20)
So it makes in another code (when i uncomment it, it lunch the code but dont show camera output) But in this code, even if I uncomment it, the code themself doesn't work: show upper error message
Here is the code
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
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)
cv2.imshow('gray',gray)
if cv2.waitKey(20) & 0xFF == ord('q'): # HERE
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
oh are you Japanese yes bro computers are little racist do not press any japanese letter while your code runing. for waitKey you can use esc or number keys:
if cv2.waitKey(20) & 0xFF == ord('0'):
break
This is the code when i execute it:
You can see the frame opens but doesnt show anything
I want to use a usb camera with a raspberry pi 3 model b v1.2 using opencv 3.3 and python 2.7.
I work with opencv in an virtual enviroment.
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
while(True):
ret, frame = cap.read() #Capture frame-by-frame
#Our operations on the frame come here
#gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
#Display resulting frame
cv2.imshow('frame',frame)
cv2.waitKey(10)
#if cv2.waitKey(1) & 0xFF == ord('q'):
# break
#when everything done, release the capture
cap.release()
cv2.destroyAllWindows()
I just have no idea how to get around this error. I already searched the error and i am getting helpless, anyone having an idea?
EDIT: i am currently playing around with the code and i can get frames but most of the time the screen stays grey. I use # to show how the code looks now
Ok, it now opens a window and shows the output of the camera
Because of this code:
import sys
sys.path.append('/home/pi/.virtualenvs/cv/lib/python2.7/site-
packages/usr/local/lib/python2.7/site-packages')
and i also use sudo python program.py in the terminal
But this Error :"NameError: name 'CV_CAP_PROP_FRAME_HEIGHT' is not defined" still persists...
I was running a simple program on reading a video file (.mp4 format) and writing it back after inverting every frame. I get an error and a bug warning.
BUG: The current event queue and the main event queue are not the same. Events will not be handled correctly. This is probably because _TSGetMainThread was called for the first time off the main thread.
Error in CoreDragRemoveTrackingHandler: -1856
I am using El Captain OSX , I have installed python virtual environment to use Python 3.5 with OpenCV 3.2.0-rc. Can somebody help me with this.?
Thanks
import numpy as np
import cv2
cap = cv2.VideoCapture('Root.mp4')
# Define the codec and create VideoWriter object
#fourcc = cv2.VideoWriter_fourcc(*'AVRN')
#out = cv2.VideoWriter('Root1.mp4',fourcc, 20.0, (640,360))
fourcc = cv2.VideoWriter_fourcc(*'MJPG')
out = cv2.VideoWriter('Root1.avi',fourcc, 20.0, (640,360))
while(cap.isOpened()):
ret, frame = cap.read()
if ret==True:
frame = cv2.flip(frame,0)
# write the flipped frame
out.write(frame)
cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
# Release everything if job is finished
cap.release()
out.release()
cv2.destroyAllWindows()
Seems to be fixed by reinstalling opencv3 with --with-ffmpeg flag.
https://github.com/opencv/opencv/issues/7474
One thing i can tell you, there is no problem in the script you mentioned above :p, it is running fine on my system. Reinstall opencv and python in new virtual environment.
Try this:
cap = cv2.VideoCapture(0)
fps = 15
capSize = (1280, 720)
#print cap.get(3)
#print cap.get(4)
fourcc = cv2.VideoWriter_fourcc('m', 'p', '4', 'v')
out = cv2.VideoWriter('output.mov',fourcc, fps, capSize, True)
It works for me, so I hope it helps you!
I'm trying to write some code for automatically capturing video from a webcam when activated through a batch script on Windows. I've managed to piece together a working script, but it does not appear to be saving the file. I know the code is (at least on a basic level) working because there are no errors and the activation light on the webcam lights up when the code is run. I've reproduced the code below if anyone has any suggestions on how to get it writing to a file, that would be great!
import numpy as np
import cv2
import msvcrt
cap = cv2.VideoCapture(0)
w=int(cap.get(cv2.CAP_PROP_FRAME_WIDTH ))
h=int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT ))
sub=raw_input("Subject#: ")
#Define the codec and create VideoWriter object
#fourcc = cv2.VideoWriter_fourcc(*'DIVX')
fourcc = cv2.VideoWriter_fourcc(*'FMP4')
out = cv2.VideoWriter('C:\path\to\output_' + sub + '.mp4', fourcc, 30, (w,h))
while(cap.isOpened()):
ret, frame = cap.read()
if ret==True:
out.write(frame)
if msvcrt.kbhit():
if ord(msvcrt.getch()) != None:
break
else:
break
#Release everything if job is finished
cap.release()
out.release()
cv2.destroyAllWindows()
The codec FMP4 is just not being supported by your camera I think. Neither in mine. If I change in your code the encoding to 'MJPG' and write to an '.avi' extension, it works very well.
P.S. Wanted to make a comment but I'm still underage (< 50).