آموزش پایتون | OpenCV (Open Source Computer Vision) یک کتابخانه پردازش تصویر کامپیوتری است که شامل عملکردهای مختلفی برای انجام عملیات بر روی تصاویر یا ویدیوها است.
زمینه های کاربردی OpenCV عبارتند از:
1) سیستم تشخیص چهره
2) ردیابی حرکت
3) شبکه عصبی مصنوعی
4) شبکه عصبی عمیق
5) پخش ویدئو و غیره
در پایتون می توان از یک کتابخانه OpenCV به نام CV2 استفاده کرد. پایتون با cv2 ارائه نمی شود، بنابراین کاربر باید آن را جداگانه نصب کند.
برای نصب OpenCV در ویندوز:
pip opencv-python
برای نصب OpenCV در لینوکس:
sudo apt-get python-opencv
از کتابخانه OpenCv می توان برای انجام چندین عملیات روی ویدیوها استفاده کرد. بیایید سعی کنیم با استفاده از CV2 کار جالبی انجام دهیم. یک ویدیو را به عنوان ورودی بگیریم و با شکستن فیلم به فریم به فریم آن را در حالت معکوس پخش کنیم و همزمان آن فریم را در لیست ذخیره کنیم. پس از دریافت لیست فریم ها، تکرار را روی فریم ها انجام می دهیم.
برای مشاهده همه مطالب آموزش پایتون کلیک کنید
کد کامل این برنامه را در زیر آمده است:
# Python program to play a video
# in reverse mode using opencv
# import cv2 library
import cv2
# videoCapture method of cv2 return video object
# Pass absolute address of video file
cap = cv2.VideoCapture("video_file_location")
# read method of video object will return
# a tuple with 1st element denotes whether
# the frame was read successfully or not,
# 2nd element is the actual frame.
# Grab the current frame.
check , vid = cap.read()
# counter variable for
# counting frames
counter = 0
# Initialize the value
# of check variable
check = True
frame_list = []
# If reached the end of the video
# then we got False value of check.
# keep looping until we
# got False value of check.
while(check == True):
# imwrite method of cv2 saves the
# image to the specified format.
cv2.imwrite("frame%d.jpg" %counter , vid)
check , vid = cap.read()
# Add each frame in the list by
# using append method of the List
frame_list.append(vid)
# increment the counter by 1
counter += 1
# last value in the frame_list is None
# because when video reaches to the end
# then false value store in check variable
# and None value is store in vide variable.
# removing the last value from the
# frame_list by using pop method of List
frame_list.pop()
# looping in the List of frames.
for frame in frame_list:
# show the frame.
cv2.imshow("Frame" , frame)
# waitkey method to stopping the frame
# for some time. q key is presses,
# stop the loop
if cv2.waitKey(25) and 0xFF == ord("q"):
break
# release method of video
# object clean the input video
cap.release()
# close any open windows
cv2.destroyAllWindows()
# reverse the order of the element
# present in the list by using
# reverse method of the List.
frame_list.reverse()
for frame in frame_list:
cv2.imshow("Frame" , frame)
if cv2.waitKey(25) and 0xFF == ord("q"):
break
cap.release()
cv2.destroyAllWindows()
دیدگاهتان را بنویسید