By, uav-jp 30/07/2022

Python (35) Let's make your own surveillance camera with OpenCV | TECH+ Mynavi News Mynavi

Last time, we introduced a simple usage from the installation of the OpenCV of the high -performance image video library.This time, let's make your own surveillance camera using OpenCV's web cameras.Here, let's make something to save an image if there is an intruder in the absent house.

Let's set up a web camera

Recent laptops have a web camera, so let's use this this time.If you don't have a web camera, you can get a USB web camera if you put out a little 2000 yen.And if OpenCV is installed, you can easily get an image of a web camera.The installation of OpenCV was introduced last time.

To get an image of a web camera in OpenCV, write the following programs."Camera".Let's save it under the name PY.

import cv2# カメラのキャプチャを開始 --- (*1)cam = cv2.VideoCapture(0)while True: # 画像を取得 --- (*2) _, img = cam.read() # ウィンドウに画像を表示 --- (*3) cv2.imshow('PUSH ENTER KEY', img) # Enterキーが押されたら終了する if cv2.waitKey(1) == 13: break# 後始末cam.release()cv2.destroyAllWindows()

And let's run from the command line.

# Windowsの場合python camera.py# macOSの場合python3 camera.py

When you execute the program, the window is displayed, where the web camera image is displayed.So when you press the Enter key, the program ends.

Check the program.In the part of ( * 1), CV2.Execute VideoCapture () to start input from the web camera.The 0 specified in the argument is the camera number, and if there are two or more cameras, the camera can be switched by changing this value.If you execute the READ () method of ( * 2), you can actually get an image from the camera.And CV2 of ( * 3).With iMshow (), the image can be displayed in the output window.

Let's make a surveillance camera that detects movement

Next, compare the images obtained from the camera continuously, notify that if there is a movement, the console has moved, and create a program to save the image in a file."Camera-Kansi".Let's save it under the name PY.

import cv2# 保存パスの指定save_path = "./"def main(): # カメラのキャプチャを開始 cam = cv2.VideoCapture(0) # フレームの初期化 --- (*1) img1 = img2 = img3 = get_image(cam) th = 300 num = 1 while True:# Enterキーが押されたら終了if cv2.waitKey(1) == 13: break# 差分を調べる --- (*2)diff = check_image(img1, img2, img3)# 差分がthの値以上なら動きがあったと判定 --- (*3)cnt = cv2.countNonZero(diff)if cnt > th:print("カメラに動きを検出")cv2.imshow('PUSH ENTER KEY', img3)# 写真を画像 --- (*4)cv2.imwrite(save_path + str(num) + ".jpg", img3)num += 1else:cv2.imshow('PUSH ENTER KEY', diff)# 比較用の画像を保存 --- (*5)img1, img2, img3 = (img2, img3, get_image(cam)) # 後始末 cam.release() cv2.destroyAllWindows() # 画像に動きがあったか調べる関数def check_image(img1, img2, img3): # グレイスケール画像に変換 --- (*6) gray1 = cv2.cvtColor(img1, cv2.COLOR_RGB2GRAY) gray2 = cv2.cvtColor(img2, cv2.COLOR_RGB2GRAY) gray3 = cv2.cvtColor(img3, cv2.COLOR_RGB2GRAY) # 絶対差分を調べる --- (*7) diff1 = cv2.absdiff(gray1, gray2) diff2 = cv2.absdiff(gray2, gray3) # 論理積を調べる --- (*8) diff_and = cv2.bitwise_and(diff1, diff2) # 白黒二値化 --- (*9) _, diff_wb = cv2.threshold(diff_and, 30, 255, cv2.THRESH_BINARY) # ノイズの除去 --- (*10) diff = cv2.medianBlur(diff_wb, 5) return diff# カメラから画像を取得するdef get_image(cam): img = cam.read()[1] img = cv2.resize(img, (600, 400)) return imgmain()

To execute the program, run from the command line as follows.

# Windowsの場合python camera-kansi.py# macOSの場合python3 camera-kansi.py

Then, a black screen will be displayed.Then, when there is a small movement, the place where the screen moved is displayed white.If there is a large movement, the screen will be replaced with a color image.At that time, the console is displayed as "detect movement in the camera" and the screen is saved in the file.

Here, three images are used to detect movement.This is famous as a method of detecting a mobile object (dynamic detection), and uses an algorithm called "Frame Aviation Difference Law".

As a method, the image is converted to a grace kale (black and white 256 gradation), and a difference image is created for each of the three images.Then, calculate the logic of the differential image.Furthermore, convert the image to a black -and -white double -valued image.Then, the part where the movement has become white becomes white, and the part without movement becomes black.

Check the specific program.In the program ( * 1), the first three images (img1, img2, img3) for comparison determining whether there was a movement was initialized.Next, in the ( * 2) part, the difference is examined by the check_image () function.In ( * 3), the difference image is examined, and the photo is saved with ( * 4) if the white part is more than the value (TH).In ( * 5), the comparison image is saved.

In the after ( * 6) part of the program, check if the image has moved.First, convert to grace kale, and ( * 7) examines the absolute difference between the three images.The argument is examined in ( * 8), ( * 9) is a black -and -white double value, and the noise is removed in ( * 10), and it is a return value of the function.

summary

This time, we introduced how to make your own surveillance camera using OpenCV.Recently, surveillance cameras are getting cheaper, but if you make your own, you can customize the operation of the surveillance camera.This time, there are many ways to save images in the file at the timing of saving, but notify by LINE or email.As I introduced this time, except for comments, even a program with less than 50 lines could make a certain surveillance camera.Let's try to create a self -made surveillance camera by using this program as a hit table.