Time-Lapse Video Creation Using Python and Webcam (with Code) | Captivating Walking Water Experiment

Let’s Tinker
Let’s Tinker
121 بار بازدید - پارسال - In this captivating time-lapse video,
In this captivating time-lapse video, we'll guide you through the mesmerizing process of creating stunning time-lapse footage using Python and your webcam. Experience the enchantment of time unfolding as we showcase a fascinating walking water experiment, where water magically transfers from one glass to another through tissue paper.

We will walk you through the entire process, from setting up the necessary Python libraries to capturing frames using your webcam and compiling them into a stunning time-lapse video. You don't need any prior programming experience to follow along, as we'll provide the code and explain each step in detail.

In this tutorial, you'll learn:

How to install the required Python library (OpenCV) for webcam integration
How to save frames as individual images for later use
How to compile the captured frames into a time-lapse video using Python

We'll also demonstrate an intriguing walking water experiment, showcasing the mesmerizing transfer of water between glasses through tissue paper. This engaging example will add an extra element of fascination to your time-lapse video creation journey.

The code provided will serve as a valuable resource for both beginners and experienced Python enthusiasts. Feel free to modify and experiment with the code to create your own unique time-lapse videos.

Unlock the power of Python and unleash your creativity by joining us on this exhilarating journey of time-lapse video creation, complete with a captivating walking water experiment. Subscribe to our channel to stay tuned for more exciting tutorials and coding adventures.

Code:
take pictures via webcam
import cv2  #pip install opencv-python
import time
import os

def capture_webcam_image(output_directory):
   Initialize webcam
   cap = cv2.VideoCapture(0)

   Check if the webcam is opened correctly
   if not cap.isOpened():
       print("Failed to open webcam")
       return

   Find the next available image number
   image_number = 1
   while True:
       output_path = f"{output_directory}/webcam_image_{image_number}.jpg"
       if not os.path.exists(output_path):
           break
       image_number += 1

   Capture and save image
   ret, frame = cap.read()
   if ret:
       cv2.imwrite(output_path, frame)
       print(f"Image saved as {output_path}")

   Release the webcam
   cap.release()

Set the output directory for the images
output_directory = "/path/to/save/images"

Set the interval between captures (in seconds)
capture_interval = 300  # 5 minutes

while True:
   Capture and save image from webcam
   capture_webcam_image(output_directory)

   Wait for the specified interval
   time.sleep(capture_interval)


#convert to video
import cv2
import os

def create_video_from_images(output_directory, output_video_path):
   Number of frames per second in the output video
   output_fps = 30

   Create the video writer object
   fourcc = cv2.VideoWriter_fourcc(*"mp4v")
   output_video = cv2.VideoWriter(output_video_path, fourcc, output_fps, (640, 480))

   Convert saved images to video
   for i in range(1, len(os.listdir(output_directory)) + 1):

       filename = "webcam_image_" + str(i) + ".jpg"
       
       if os.path.exists(filename):
           image_path = os.path.join(output_directory, filename)
           frame = cv2.imread(image_path)
           output_video.write(frame)

   Release the video writer
   output_video.release()

   print(f"Video saved as {output_video_path}")

Set the output directory for the images
output_directory = "/path/to/save/images"
Set the output video path
output_video_path = "output_video.mp4"

Convert saved images to video
create_video_from_images(output_directory, output_video_path)


Subscribe to our channel:
@kr9c
پارسال در تاریخ 1402/03/08 منتشر شده است.
121 بـار بازدید شده
... بیشتر