📌 عنوان برنامه: برنامه‌ای بنویسید که Webcam متصل به دستگاه را روشن کرده و هر ثانیه یک‌بار، یک عکس از تصویر موجود در Webcam گرفته و با تصویر قبل آن‌را مقایسه کند و اگر تغییری در تصویر جدید بوجود آمده بود (Motion Detection)، تصویر مربوطه را با توجه به تاریخ و زمان سیستم، و با فرمت png ذخیره نماید؟

نکته: برای درک فلسفه این کد، به مطلب قبلی مراجعه نمایید!

# **************************************************
# pip install opencv-python
# https://github.com/opencv/opencv-python
# **************************************************
import time
import cv2 as cv
import numpy as np
from datetime import datetime

capture = cv.VideoCapture(index=0)

# Check if the webcam is opened correctly
if not capture.isOpened():
print("[#] Can not open webcam!")
quit()

last_mean = None

while True:
time.sleep(1)

# Frame means Webcam Image Capture!
result, frame = capture.read()

if result:
# Converting color image to gray_scale image
gray = cv.cvtColor(src=frame, code=cv.COLOR_BGR2GRAY)

# In first iteration we assign the
# value of 'old_gray' to our first frame
if last_mean is None:
last_mean = np.mean(gray)
continue

difference = np.abs(np.mean(gray) - last_mean)
# print(difference)

last_mean = np.mean(gray)

if difference > 0.3:
print("Motion Detected!")

now = datetime.now()
formated_now = now.strftime("%Y_%m_%d_%H_%M_%S")
filename = f"Capture_{formated_now}.png"
cv.imwrite(filename=filename, img=frame)

# Wait 1ms for ESC to be pressed
key = cv.waitKey(delay=1)
if key == 27:
break

capture.release()
cv.destroyAllWindows()
# **************************************************

#SourceCode #SourceCode10030 #Practical
کانال پایتون:
@DT_PYTHON_LEARNING
ادمین:
@Dariush_Tasdighi
کانال اصلی:
@IranianExperts
.
8👍4