Summer Sale: 5% OFF Over $49, Code: Elephant5; 10% OFF Over $99, Code:Elephant10



HD USB drive-free camera for Raspberry Pi Tutorials

Posted by Fiona Su on

1.1 Camera webpage access tutorial

Part 1--Connect camera into Raspberry Pi board

1. User must ensure that the camera is properly inserted into the Raspberry Pi board before supplying power to the Raspberry Pi board.

2. There are two kinds of commands that the Raspberry Pi terminal can use to detect external camera devices: "lsusb" and "ls/dev/video*". (In some cases, you may be able to use the camera without seeing video0 service)

After we input following command, we can detect camera service.

As shown below.

image.png

Part 2 -- Upgrading the system

You must to install a new operating  system before use camera, which can  can identify if the camera module is connected. The simplest method is download Raspbain system from Raspberry Pi website, and install it to new SD card.

Regardless of the version of the Raspbian system you are using, I recommend that you update the system with the following command:

sudo apt-get update

sudo apt-get upgrade

Part 3 -- Open the camera service of Raspberry Pi

1. If you use Raspberry Pi official system image, you need to open the camera service. Input sudo raspi-config command to enter following interface.

2. Choose fifth option [Interfacing Options] and click [Enter] key to enter next interface.

image.png

3. ChoOse [Camera] to enable camera.

image.png 4. Finally, we need to reboot Raspberry Pi.

Input sudo reboot command.

Part 4 -- Using camera

1.You can transfer the video which captured by the Raspberry Pi to the web page.

You need to log in to the WinSCP software and transfer the master.zip file to the pi directory of the Raspberry Pi.image.png

2.You should input: #unzip master.zip

(This command to unzip mater.zip)

After complete unzip, input ls, you can see migp-streamer-master folder. As shown in the figure below.

image.png 3.Compiling this project requires cmake: perform the following command to install download.

(apt-get is a networked installation, so you need make Raspberry Pi successfully connect to the network.)

You should input:

#sudo apt-get install cmake  

A prompt will appear when installing, you need to enter Y.

After completion, As shown in the figure below.

image.png

4. You need to install library

You should input:

#sudo apt-get install libjpeg8-dev  

After completion, As shown in the figure below.

image.png

5.Compiling

Note: we possess two formats of camera (JPEG/YUYV )

2-degree-of-freedom camera: YUYV/JPEG

If we want to change to the YUYV format, we need to modify the relevant files and compile:

Specific steps as shown below:

You should enter the input_uvc.c file in the /home/pi/mjpg-streamer-master/mjpg-streamer-experimental/plugins/input_uvc/ directory. 

You should input:

cd /home/pi/mjpg-streamer-master/mjpg-streamer-experimental/plugins/input_uvc/

You should input:

 ls

image.png

Just modify the format of 135 lines to V4L2_PIX_FMT_YUYV (The default is format=V4L2_PIX_FMT_MJPEG)

You should input command

nano -c input_uvc.c

image.png

If you want to change the resolution and frame rate of the USB camera, you can change it here.

After the modification is completed, press ctrl+X, press Y to save, and then press the Enter key.

Then return to the mjpg-streamer-experimental folder to compile

Then return to mjpg-streamer-experimental:

cd /home/pi/mjpg-streamer-master/mjpg-streamer-experimental

And input make clean all to complete the compilation.

image.png

You can wait for the compilation to complete, you can see the interface shown below.

image.png

6.Restart system

You need to input: sudo reboot  to reboot system

Plug in the camera and restart the system.

7. After rebooting, enter the system.

You need to  enter the mjpg-streamer-experimental directory by command. And use the following command to start the normal USB camera (for 2-DOF cameras):

#./mjpg_streamer -i "./input_uvc.so" -o "./output_http.so -w ./www"  

Some cameras will report an error when executing this command. If they do not return to the command prompt and display "Starting Camera", it means success.

As shown in the figure below, the camera is successfully turned on:

image.png 8. Testing

View the image, open the browser on the PC side, you need to enter the following URL to see the static screenshot:

http://<RaspberryPi IP>:8080/?action=snapshot  

My URL ishttp://192.168.0.179:8080/?action=snapshot  

As shown below.

image.png

 You should input the following URL to see the dynamic image:

http://<RaspberryPi IP>:8080/javascript_simple.html  

My URL ishttp://192.168.1.179:8080/javascript_simple.html

image.png 

Note: After running the camera web page service, this process will occupy the camera, causing other camera commands to fail to run. Please end the process before running other camera commands.

View camera process number:

ps a

image.png Input following command to kill PID

sudo kill -9 1118

image.png

Different Raspberry Pi process numbers are different. Please refer to the process shown in your own system.

1.2 Camera capture pictures

Part 1-Open the camera service of Raspberry Pi

1. If you use Raspberry Pi official system image, you need to open the camera service. Input sudo raspi-config command to enter following interface.

2. Choose fifth option [Interfacing Options] and click [Enter] key to enter next interface.

image.png 3. ChoOse [Camera] to enable camera.

image.png

4. Finally, we need to reboot Raspberry Pi.

Input sudo reboot command.

Part 2-- Using camera to capture pictures

1. User must ensure that the camera is properly inserted into the Raspberry Pi board before supplying power to the Raspberry Pi board.

2. There are two kinds of commands that the Raspberry Pi terminal can use to detect external camera devices: "lsusb" and "ls/dev/video*". (In some cases, you may be able to use the camera without seeing video0 service)

After we input following command, we can detect camera service.

As shown below.

image.png 3. Input following command to install mplayer player.

sudo apt-get install mplayer -y

Wait patiently, after the installation is complete, you can see the interface shown below.

image.png 4. Input following command to install fswebcam video software.

sudo apt-get install fswebcam -y

image.png Input following command to view USB camera picture.

sudo mplayer tv://

5. After confirming the screen, you need to exit through ctrl+c before proceeding to the next operation.

If you run mplayer and use the fswebcam command at the same time, the system will prompt an error that the camera is busy. As shown below.

image.png

6. Input following command to generate a real-time photo taken by the current camera in the /home/pi directory

fswebcam -d /dev/video0 --no-banner -r 320x240 -S 10 /home/pi/image.jpg

image.png Parameter explanation:

-d -- configure which camera device to use

--no-banner --- There is no watermark in the photos taken. If this parameter is not used, the system may prompt a wrong font

-r --  Size of picture

-S -- Visibility, the range is from 1 to 10. If this parameter is not set or this parameter is set to 0, the photo will be black.

/home/pi/image.jpg -- Save the image path (if you do not add the path, picture will be saved in the current directory /home/pi/ by default ).

1.3 Camera python driver tutorial

Common API functions used by OpenCV:

1. cv2.VideoCapture()

cap = cv2.VideoCapture(0)

The parameter in VideoCapture () is 0, which means Raspberry Pi video0.

(Note: You can view the current camera through the command ls/dev/ )

image.png

cap = cv2.VideoCapture("…/1.avi")

VideoCapture("…/1.avi"), This parameter indicates that if the path of the video file is entered, the video is opened.

2.cap.set()

Camera parameters common configuration methods:

capture.set(CV_CAP_PROP_FRAME_WIDTH, 1920); # Width

capture.set(CV_CAP_PROP_FRAME_HEIGHT, 1080); # Height

capture.set(CV_CAP_PROP_FPS, 30); # Frame

capture.set(CV_CAP_PROP_BRIGHTNESS, 1); # Brightness 1

capture.set(CV_CAP_PROP_CONTRAST,40); # Contrast 40

capture.set(CV_CAP_PROP_SATURATION, 50); # Saturation 50

capture.set(CV_CAP_PROP_HUE, 50);
# Hue 50

capture.set(CV_CAP_PROP_EXPOSURE, 50);
# Visibility 50

Parameter explanation:

#define CV_CAP_PROP_POS_MSEC 0

// Calculate the current position in milliseconds

#define CV_CAP_PROP_POS_FRAMES 1

// Calculate the current position in frame

#define CV_CAP_PROP_POS_AVI_RATIO 2 // Relative position of the video

#define CV_CAP_PROP_FRAME_WIDTH 3 // Width

#define CV_CAP_PROP_FRAME_HEIGHT 4 // Height

#define CV_CAP_PROP_FPS 5 // Frame rate

#define CV_CAP_PROP_FOURCC 6 // 4 Character encoding

#define CV_CAP_PROP_FRAME_COUNT 7 // Video frames

#define CV_CAP_PROP_FORMAT 8 // Video format

#define CV_CAP_PROP_MODE 9

// Backend specific value indicating the current capture mode.

#define CV_CAP_PROP_BRIGHTNESS 10 // Brightness

#define CV_CAP_PROP_CONTRAST 11 // Contrast

#define CV_CAP_PROP_SATURATION 12 // Saturation

#define CV_CAP_PROP_HUE 13 // Hue

#define CV_CAP_PROP_GAIN 14 // Gain

#define CV_CAP_PROP_EXPOSURE 15 // Exposure

#define CV_CAP_PROP_CONVERT_RGB 16

// Mark whether the image should be converted to RGB.

#define CV_CAP_PROP_WHITE_BALANCE 17 // White balance

#define CV_CAP_PROP_RECTIFICATION 18 // Stereo camera calibration mark (note: only support DC1394 v2)

3.cap.isOpened()

Return true indicates open camera successful and false indicates open camera failure

4.ret,frame = cap.read()

cap.read () reads the video frame by frame. ret and frame are the two return values of the cap.read () function.

ret is a Boolean value, if the read frame is correct, it will return true, If the file has not been read to the end, it returns False.

Frame is the image of each frame, which is a three-dimensional matrix.

5.cv2.waitKey(n)

n represents the delay time, if the parameter is 1, it means a delay of 1ms to switch to the next frame of image.

If the parameter is too large, such as cv2.waitKey (1000), it will freeze because of the long delay.

The parameter is 0, such as, cv2.waitKey (0) only displays the current frame image, which is equivalent to video pause.

6.cap.release() and destroyAllWindows()

Call cap.release () to release the video.

Call destroyAllWindows () to close all image windows.

About Code

Since our entire tutorial runs in JupyterLab, we must understand the various components inside.

Here we need to use the image display component.

1.Import library

import ipywidgets.widgets as widgets

2.Set Image component

image_widget = widgets.Image(format='jpeg', width=600, height=500)

3.Display Image component

display(image_widget)

4.Open camera and read image

image = cv2.VideoCapture(0) # Open camera

ret, frame = image.read() # Read camera data

5.Assignment to components

#Convert the image to jpeg and assign it to the video display component

image_widget.value = bgr8_to_jpeg(frame)

import cv2

import ipywidgets.widgets as widgets

import threading

import time

#Set camera display component

image_widget = widgets.Image(format='jpeg', width=600, height=500)

display(image_widget) # display camera component

#bgr 8 to jpeg format

import enum

import cv2

def bgr8_to_jpeg(value, quality=75):

return bytes(cv2.imencode('.jpg', value)[1])

image = cv2.VideoCapture(0) # Open camera

# width=1280

# height=960

# cap.set(cv2.CAP_PROP_FRAME_WIDTH,width) # set width of image

# cap.set(cv2.CAP_PROP_FRAME_HEIGHT,height) # set height of image

image.set(3,600)

image.set(4,500)

image.set(5, 30) # set frame

image.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter.fourcc('M', 'J', 'P', 'G'))

image.set(cv2.CAP_PROP_BRIGHTNESS, 40) #set brightness -64 - 64 0.0

image.set(cv2.CAP_PROP_CONTRAST, 50) #set contrast -64 - 64 2.0

image.set(cv2.CAP_PROP_EXPOSURE, 156) #set exposure value 1.0 - 5000 156.0

ret, frame = image.read() # read camera data

image_widget.value = bgr8_to_jpeg(frame)

while 1:

ret, frame = image.read()

image_widget.value = bgr8_to_jpeg(frame)

time.sleep(0.010)

image.release() #After using the object, we need to release the object, otherwise when we use the object again, the system will prompt that the object be occupied, making it unusable.

The camera screen is shown below.

image.png 

Download

0 comments

Leave a comment

Please note, comments must be approved before they are published