How to stop SimpleCV camera stream? - python

I'm trying to learn SimpleCV using Python 2.7 in IDLE.
Once the camera form SimpleCV is initialized the camera become unavailable to other programs like native webcam application or skype etc.
from SimpleCV import *
camera = Camera()
After restarting the pc or logoff and logon the webcam becomes to those applications. It seems that even closing out from python IDLE, it doesn't close the camera stream. Is there any way to stop the camera stream of simplecv?

I couldn't replicate your issue, but if the webcam is still running even after your program terminates/you close IDLE, you can end the camera by going into task manager and killing all running Python processes.
After some experimenting, I found that if you want to accomplish the same thing directly inside the code, you could try simply deleting the reference altogether:
>>> import SimpleCV as scv
>>> cam = scv.Camera()
>>> del cam
Calling del cam caused the webcam indicator light on my laptop to turn off. Granted, this appears to be an undocumented (??) solution, so I'm not sure how robust it is. I would probably try testing this on several different laptops/webcams first, to make sure it works reliably and consistently.

Related

How can I run two python script using the same pi camera in raspberry pi?

I am currently working on a project where I need to have a live stream video and a motion detection that when a motion is detected, the raspberry pi records a 10 second video. I have put each functionality in different python files. I am using a single Pi camera. I have also created a file that consists of all the camera functionality so that I don't need to initialize the picamera twice. In the live stream file there is a button that allows the user to activate the motion detection functionality. When the button is clicked an error is shown "Failed to enable connection: Out of Resources"(Note that the live stream is already running.). I just wanted to know if it is possible to run two programs simultaneously using a single pi camera. If it is possible, how? Any help would be appreciated. Thank you in advance.
In general, AFAIK, it is not possible to access a single Raspberry Pi camera simultaneously from 2 separate processes.
I see a couple of possibilities which I'll describe in separate sections.
Use the shell and its tee command, to duplicate your video stream to 2 separate processes like this:
raspivid ... | tee >(MotionDetectionProcess) >(LiveStreamingProcess) > /dev/null
This is a bash "process substitution".
You might use raspividyuv or ffmpeg or some other program, or even one written by yourself, to read the camera and pass the data to stdout.
This approach is very quick to set up and simple - everything just reads or writes its stdin or stdout. On the downside, everything must be started at the same time and things run in "lock-step" with each other.
Run a single video capture process, that simply writes the video frames into multiprocessing shared memory.
Write your other two programs separately, but such that they "attach" to the shared memory and get the frames from there rather than directly from the camera.
This approach is initially a bit harder to program, but things are more decoupled, so you may choose not to bother running motion detection one day, or to start it later or sooner, and your live stream will be unaffected.

Is there a way/ method to check/ return a value everytime the camera gets accessed by any of the processes/ apps using open-cv Python?

I am trying to build a Python script that returns True every time the camera gets opened by an app and I came across several Open-CV scripts but could not get them working for this purpose. Also I tried using the cam.isOpened() inside an infinite loop to check if it worked while accessing the camera through another app, as given below, but it did not work.
import numpy as np
import cv2
cam = cv2.VideoCapture(0)
while(True):
if cam.isOpened():
return("True")
Is there a way this task could be achieved?
OpenCV can't do this. it's not meant to. you're using its APIs wrong.
you need operating system APIs if you want to check whether anything currently accesses a camera.

opencv python camera permission issue on Windows 10

While running a simple opencv video capture script, i am getting False as the result. I suspect it is due to some security setting in Windows 10 which is not allowing camera access. I checked Privacy > Camera settings, but there was no option to allow a script to access the camera. I can see that the camera is not turned on when running the following opencv based test script.
import cv2
cap = cv2.VideoCapture(0)
while(True):
ret, frame = cap.read()
print(ret)
The Key to the answer is "Give time for Microsoft Windows to initalize WebCAM"
import time
capWebcam = cv2.VideoCapture(0)
time.sleep(1.000) # Make sure, you need to give time
# for MS Windows to initialize Camera
It's called "Allow access to classic application" or "Desktop applications" something like this in the bottom of the setting page, under Windows Store type applications.
This gain camera access to all EXE and DLL standalone applications.
One setting for all of them.
More info on exceptions here https://support.microsoft.com/en-us/help/4468234/windows-10-desktop-apps-and-privacy
Works for me in
'cv2.__version__ 4.2.0'
just installed latest opencv and python8 on latest windows10.
As suggested in previous helpful answers, after checking windows camera security setting, adding time delay, and running windows camera app, the program works fine.

Why won't a .mpg video load into pygame's movie module

I've been trying to get a short video to load and play within a pygame window. However, the program stalls when it tries to load the video. I'm also unable to abort the debugger.
import pygame
import pygame.movie
pygame.init()
print('TEST')
video = pygame.movie.Movie('D:\\Presentation\\video.mpg')
print('TEST')
This outputs 'TEST' once, so the video has not loaded (It keeps running "pygame.movie.Movie('D:\Presentation\video.mpg')" indefinitely).
I let it run for an hour but no luck, it keeps loading without any kind of exception or visible progress and the video is 1,636 KB large, 4 seconds long and was converted from a .mp4 using ffmpeg.
The directory is certainly correct for I tested with images in the same directive. I also restarted my PC (Windows 8.1 64-bit Python 3.2.5.1).
EDIT: I need an answer that will be relevant to the pygame module
I suggest you use Pyglet and follow the code I create here that plays a video.

v4l2 / streamer unstable in operation

I am making an online webcam for my parents, using raspberry pi. I want it to capture a photo, upload it to a webserver, then upload a copy to a different server for archiving. I use the script streamer to snap a still from the webcam. It works, the problem is that it seems that streamer sometimes crashes, looping the error message "v4l2: oops: select timeout". This can happen after a few shots, or after 10 minutes of operation, it seems random. I have added a command that kills the streamer process after each snapshot, it did make the program a bit more stable, but eventually it still gets stuck in the error loop. I don't know what the problem is or even how to debug it.. What can I do?
I am using raspbian with the included drivers. The webcam is logitech c200. I first tried using opencv to capture stills, but couldn't get it to work properly. If someone could help with that maybe it would fix the problem, I don't know..
This is the code, it's python:
import time
import sys
from subprocess import call
import ftputil
while True:
call("streamer -q -f jpeg -s 640x480 -o ./current.jpeg", shell=True)
time.sleep(0.2);
call("killall -q streamer", shell=True)
filename = str(time.time()) + ".jpg"
host = ftputil.FTPHost(*****)
#host.remove("/domains/***/public_html/webcam.jpg")
host.upload("./current.jpeg", "/domains/***/public_html/webcam.jpg", mode='b')
host.close()
host = ftputil.FTPHost(****)
#host.remove("/domains/***/public_html/webcam.jpg")
host.upload("./current.jpeg", "/webcamarchive/"+filename, mode='b')
host.close()
time.sleep(10);
Never mind, used pygame instead:
cam = pygame.camera.Camera("/dev/video0",(640,480))
cam.start()
image = cam.get_image()

Categories