I will introduce how to easily acquire a camera image using opencv and how to execute it regularly. The cameras here are limited to cameras that can be controlled by opencv, such as USB cameras and built-in cameras. Settings etc. are based on the premise that you are using python with anaconda3 series. It works on any OS such as windows, mac and linux.
If you want to perform automatic control with a high-end camera such as a single-lens reflex camera, gphoto seems to be a must. The gphoto compatible models are at http://www.gphoto.org/proj/libgphoto2/support.php, and quite a few are supported. Control SLR camera from Raspberry Pi is helpful.
Since anaconda does not include opencv by default,
python
conda install -c conda-forge opencv
Execute and enter.
schedule
This is not included in anaconda, so you can use pip.
python
pip install schedule
The reason why anaconda is not included depends on the details of development, so there is no need to pursue deeply.
oneshot.py
#!/usr/bin/env python
import cv2
deviceid=0 # it depends on the order of USB connection.
capture = cv2.VideoCapture(deviceid)
ret, frame = capture.read()
cv2.imwrite('test.jpg', frame)
--Capture = cv2. Get the device ID with VideoCapture (deviceid). However, note that the device ID changes depending on the order in which it is pointed, in the case of a USB camera. Normally, number 0 should be built-in, and USB camera should be number 1. --ret, frame = capture.read () At this moment, the image data is saved in frame. --Use cv2.imwrite ('test.jpg', frame) to save the acquired image in jpg. The save file format is determined by the extension.
For how to save images with OpenCV, refer to Reading and saving image files with Python, OpenCV. Capture video of Mac built-in camera with python can be used as a reference for determining errors and changing to grayscale.
In the directory where oneshot.py is located, in an environment where python has a PATH
python
python oneshot.py
Or
python
chmod +x oneshot.py
./oneshot.py
is. If you are using linux or mac, use the terminal. Windows can be a command prompt, but it may be easier to run it from an IDE-like app.
scheduled_shot.py
#!/usr/bin/env python
import cv2
import datetime
import schedule
import time
deviceid=0 # it depends on the order of USB connection.
capture = cv2.VideoCapture(deviceid)
def job():
ret, frame = capture.read()
strdate=datetime.datetime.now().strftime('%Y%m%dT%H%M%S')
fname="image_" + strdate + ".jpg "
cv2.imwrite(fname, frame)
print(fname + " is created.")
#do job every 10 seconds
schedule.every(1/6).minutes.do(job)
while True:
schedule.run_pending()
time.sleep(1)
This time, the image is acquired once every 10 seconds. For how to use schedule, refer to Let's execute the module periodically using schedule.
The while True writing style may not be common in software, but it is a must-have writing style for hardware control. With this, you can implement a function that creates an infinite loop, for example, in the case of a rice cooker, if you point to the power cord, it will continue to work. Here, with While True, you can continue to take images once every 10 seconds indefinitely. To escape from the infinite loop, you need to forcibly terminate the program with cntl + c etc. (In this way of writing, you can also set a timer to terminate after a certain period of time).
Generate the file name using datetime so that the file name is not rewritten and the file names are arranged in chronological order. Please refer to Convert date, time and character string with Python datetime.
In the case of continuous acquisition, once you have acquired the image, check the file size and consider how much space you need. If you notice, the remaining capacity of the disk is often zero.
python
python scheduled_shot.py
...
image_20201001T110144.jpg is created.
image_20201001T110154.jpg is created.
image_20201001T110204.jpg is created.
From now on, when executed, the image will be saved every 10 seconds. Since there is only forced termination, it is terminated by forcibly terminating the program with cntl + c or the like.
When I try to view an image with imshow of python etc., the output of recent images is quite slow because of the large capacity. Save the image in png or jpeg before viewing it.
If this is not enough to use, [Let's make a GUI with PyQt5! You can easily create a GUI such as Explanation from introduction to usage. However, from the viewpoint of stability, it is better to make it work properly with CUI.
Also, in such a program, it is better to concentrate the data acquisition program on it. The CPU and programs that acquire and save images from the camera should concentrate on not losing the data. Secondary operations such as downloading, transferring, and processing data in the latter stage are performed by another program.
Recommended Posts