Untitled
unknown
plain_text
9 months ago
3.9 kB
14
Indexable
import streamlit as st
import moviepy.editor as mp
import cv2
import numpy as np
from tensorflow.keras.applications import MobileNetV2
from tensorflow.keras.applications.mobilenet_v2 import preprocess_input, decode_predictions
# Load pre-trained MobileNetV2 model for object detection
model = MobileNetV2(weights="imagenet")
def detect_objects(frame):
"""
Detect objects in a video frame using MobileNetV2.
"""
resized_frame = cv2.resize(frame, (224, 224))
preprocessed_frame = preprocess_input(resized_frame)
predictions = model.predict(np.expand_dims(preprocessed_frame, axis=0))
decoded_predictions = decode_predictions(predictions, top=3)[0]
return decoded_predictions
def apply_object_detection(video_path, output_path):
"""
Apply object detection to a video and save the output.
"""
cap = cv2.VideoCapture(video_path)
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = int(cap.get(cv2.CAP_PROP_FPS))
fourcc = cv2.VideoWriter_fourcc(*"mp4v")
out = cv2.VideoWriter(output_path, fourcc, fps, (width, height))
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
# Detect objects in the frame
predictions = detect_objects(frame)
for _, label, confidence in predictions:
cv2.putText(frame, f"{label}: {confidence:.2f}", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
# Write the frame to the output video
out.write(frame)
cap.release()
out.release()
def main():
st.title(")Editor is loading...
Leave a Comment