File size: 2,988 Bytes
232b6be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b0c745c
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import easyocr
import numpy as np
import cv2
from PIL import Image
import gradio as gr
import tempfile
import os

# Initialize EasyOCR reader globally
reader = easyocr.Reader(['en'])

def process_frame(frame):
    try:
        # Convert frame to BGR for OpenCV processing
        image_bgr = cv2.cvtColor(np.array(frame), cv2.COLOR_RGB2BGR)

        # Perform OCR on the entire frame
        results = reader.readtext(image_bgr)

        # List to hold the detected texts
        detected_texts = []

        # Draw bounding boxes and text on the frame
        for (bbox, text, prob) in results:
            detected_texts.append(text)
            (x1, y1), (x2, y2), (x3, y3), (x4, y4) = bbox
            cv2.rectangle(image_bgr, (int(x1), int(y1)), (int(x3), int(y3)), (0, 255, 0), 2)
            cv2.putText(image_bgr, text, (int(x1), int(y1) - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)

        # Convert BGR frame back to RGB
        frame_rgb = cv2.cvtColor(image_bgr, cv2.COLOR_BGR2RGB)
        # Convert the frame back to PIL Image format
        result_image = Image.fromarray(frame_rgb)

        # Join all detected texts into a single string
        result_text = "\n".join(detected_texts)

        return result_image, result_text
    except Exception as e:
        return None, f"Error processing frame: {str(e)}"

def process_video(video_file_path):
    try:
        # Create a temporary file for output
        output_path = os.path.join(tempfile.gettempdir(), 'output.avi')

        # Open the video file
        cap = cv2.VideoCapture(video_file_path)
        if not cap.isOpened():
            raise ValueError("Error opening video file.")

        # Get video properties
        width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
        height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
        fps = cap.get(cv2.CAP_PROP_FPS)

        # Define the codec and create VideoWriter object
        fourcc = cv2.VideoWriter_fourcc(*'XVID')
        out = cv2.VideoWriter(output_path, fourcc, fps, (width, height))

        # Process each frame
        detected_texts = []
        while cap.isOpened():
            ret, frame = cap.read()
            if not ret:
                break

            result_image, result_text = process_frame(frame)
            if result_image is None:
                raise ValueError(result_text)  # Pass error message from frame processing
            detected_texts.append(result_text)
            out.write(np.array(result_image))

        cap.release()
        out.release()

        # Join all detected texts into a single string
        full_text = "\n".join(detected_texts)

        return output_path, full_text
    except Exception as e:
        return None, f"Error processing video: {str(e)}"

# Create Gradio interface
iface = gr.Interface(
    fn=process_video,
    inputs=gr.Video(),  # Input video file
    outputs=[gr.Video(), gr.Textbox()]  # Output video file and detected text
)

# Launch the interface
iface.launch()