| import cv2 |
| import gradio as gr |
| import os |
|
|
| |
| video_path = "" |
| live_mode = False |
|
|
| |
| save_dir = "/tmp/videos" |
| if not os.path.exists(save_dir): |
| os.makedirs(save_dir) |
|
|
| def upload_video(uploaded_video): |
| global video_path, live_mode |
| live_mode = False |
| video_path = os.path.join(save_dir, "uploaded_video.mp4") |
|
|
| |
| with open(video_path, "wb") as f: |
| f.write(uploaded_video.read()) |
| |
| return f"वीडियो अपलोड हुआ: {video_path}" |
|
|
| def start_live_feed(): |
| global live_mode, video_path |
| live_mode = True |
| video_path = 0 |
| return "लाइव फीड शुरू हो गया" |
|
|
| def process_video(): |
| if video_path == "": |
| return "कोई वीडियो अपलोड नहीं हुआ है या लाइव मोड सक्रिय नहीं है। कृपया पहले वीडियो अपलोड करें या लाइव मोड सक्रिय करें।", None |
|
|
| cap = cv2.VideoCapture(video_path) |
| if not cap.isOpened(): |
| return "त्रुटि: वीडियो खोलने में असमर्थ", None |
|
|
| processed_frame = None |
| while True: |
| ret, frame = cap.read() |
| if not ret: |
| break |
|
|
| |
| gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) |
| edges = cv2.Canny(gray_frame, 50, 150) |
|
|
| |
| contours, _ = cv2.findContours(edges, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) |
| for cnt in contours: |
| x, y, w, h = cv2.boundingRect(cnt) |
| cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 2) |
|
|
| processed_frame = frame |
|
|
| cap.release() |
| if processed_frame is not None: |
| processed_image_path = os.path.join(save_dir, "processed_frame.png") |
| cv2.imwrite(processed_image_path, processed_frame) |
| return None, processed_image_path |
| else: |
| return "कोई चार्ट नहीं मिला या वीडियो खाली है।", None |
|
|
| def end_processing(): |
| global video_path, live_mode |
| video_path = "" |
| live_mode = False |
| return "प्रसंस्करण समाप्त हुआ। आप अब नया वीडियो अपलोड कर सकते हैं या लाइव फीड शुरू कर सकते हैं।" |
|
|
| |
| iface = gr.Blocks() |
|
|
| with iface: |
| gr.Markdown("## वीडियो और लाइव फीड चार्ट एनालिसिस टूल") |
| |
| with gr.Row(): |
| video_input = gr.Video(label="वीडियो अपलोड करें") |
| upload_button = gr.Button("वीडियो अपलोड करें") |
| live_button = gr.Button("लाइव फीड शुरू करें") |
| upload_output = gr.Textbox(label="अपलोड स्थिति") |
| |
| upload_button.click(upload_video, inputs=video_input, outputs=upload_output) |
| live_button.click(start_live_feed, outputs=upload_output) |
| |
| with gr.Row(): |
| process_button = gr.Button("वीडियो प्रोसेस करें") |
| end_button = gr.Button("प्रसंस्करण समाप्त करें") |
| process_output = gr.Image(label="प्रसंस्कृत वीडियो") |
| process_status = gr.Textbox(label="प्रसंस्करण स्थिति") |
| end_output = gr.Textbox(label="प्रसंस्करण स्थिति") |
|
|
| process_button.click(process_video, outputs=[process_status, process_output]) |
| end_button.click(end_processing, outputs=end_output) |
|
|
| iface.launch() |
|
|