Spaces:
Runtime error
Runtime error
Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
"""FinalSAFEAI.ipynb
|
| 3 |
+
|
| 4 |
+
Automatically generated by Colab.
|
| 5 |
+
|
| 6 |
+
Original file is located at
|
| 7 |
+
https://colab.research.google.com/drive/1tVl88TtU7LO98RDhv3kCnXs8EbaC8BUF
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
pip install easyocr numpy opencv-python pillow gradio
|
| 11 |
+
|
| 12 |
+
import easyocr
|
| 13 |
+
import numpy as np
|
| 14 |
+
import cv2
|
| 15 |
+
from PIL import Image
|
| 16 |
+
import gradio as gr
|
| 17 |
+
|
| 18 |
+
print("All modules imported successfully!")
|
| 19 |
+
|
| 20 |
+
pip install --upgrade gradio
|
| 21 |
+
|
| 22 |
+
import easyocr
|
| 23 |
+
import numpy as np
|
| 24 |
+
import cv2
|
| 25 |
+
from PIL import Image
|
| 26 |
+
import gradio as gr
|
| 27 |
+
import tempfile
|
| 28 |
+
import os
|
| 29 |
+
|
| 30 |
+
# Initialize EasyOCR reader globally
|
| 31 |
+
reader = easyocr.Reader(['en'])
|
| 32 |
+
|
| 33 |
+
def process_frame(frame):
|
| 34 |
+
try:
|
| 35 |
+
# Convert frame to BGR for OpenCV processing
|
| 36 |
+
image_bgr = cv2.cvtColor(np.array(frame), cv2.COLOR_RGB2BGR)
|
| 37 |
+
|
| 38 |
+
# Perform OCR on the entire frame
|
| 39 |
+
results = reader.readtext(image_bgr)
|
| 40 |
+
|
| 41 |
+
# List to hold the detected texts
|
| 42 |
+
detected_texts = []
|
| 43 |
+
|
| 44 |
+
# Draw bounding boxes and text on the frame
|
| 45 |
+
for (bbox, text, prob) in results:
|
| 46 |
+
detected_texts.append(text)
|
| 47 |
+
(x1, y1), (x2, y2), (x3, y3), (x4, y4) = bbox
|
| 48 |
+
cv2.rectangle(image_bgr, (int(x1), int(y1)), (int(x3), int(y3)), (0, 255, 0), 2)
|
| 49 |
+
cv2.putText(image_bgr, text, (int(x1), int(y1) - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)
|
| 50 |
+
|
| 51 |
+
# Convert BGR frame back to RGB
|
| 52 |
+
frame_rgb = cv2.cvtColor(image_bgr, cv2.COLOR_BGR2RGB)
|
| 53 |
+
# Convert the frame back to PIL Image format
|
| 54 |
+
result_image = Image.fromarray(frame_rgb)
|
| 55 |
+
|
| 56 |
+
# Join all detected texts into a single string
|
| 57 |
+
result_text = "\n".join(detected_texts)
|
| 58 |
+
|
| 59 |
+
return result_image, result_text
|
| 60 |
+
except Exception as e:
|
| 61 |
+
return None, f"Error processing frame: {str(e)}"
|
| 62 |
+
|
| 63 |
+
def process_video(video_file_path):
|
| 64 |
+
try:
|
| 65 |
+
# Create a temporary file for output
|
| 66 |
+
output_path = os.path.join(tempfile.gettempdir(), 'output.avi')
|
| 67 |
+
|
| 68 |
+
# Open the video file
|
| 69 |
+
cap = cv2.VideoCapture(video_file_path)
|
| 70 |
+
if not cap.isOpened():
|
| 71 |
+
raise ValueError("Error opening video file.")
|
| 72 |
+
|
| 73 |
+
# Get video properties
|
| 74 |
+
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 75 |
+
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 76 |
+
fps = cap.get(cv2.CAP_PROP_FPS)
|
| 77 |
+
|
| 78 |
+
# Define the codec and create VideoWriter object
|
| 79 |
+
fourcc = cv2.VideoWriter_fourcc(*'XVID')
|
| 80 |
+
out = cv2.VideoWriter(output_path, fourcc, fps, (width, height))
|
| 81 |
+
|
| 82 |
+
# Process each frame
|
| 83 |
+
detected_texts = []
|
| 84 |
+
while cap.isOpened():
|
| 85 |
+
ret, frame = cap.read()
|
| 86 |
+
if not ret:
|
| 87 |
+
break
|
| 88 |
+
|
| 89 |
+
result_image, result_text = process_frame(frame)
|
| 90 |
+
if result_image is None:
|
| 91 |
+
raise ValueError(result_text) # Pass error message from frame processing
|
| 92 |
+
detected_texts.append(result_text)
|
| 93 |
+
out.write(np.array(result_image))
|
| 94 |
+
|
| 95 |
+
cap.release()
|
| 96 |
+
out.release()
|
| 97 |
+
|
| 98 |
+
# Join all detected texts into a single string
|
| 99 |
+
full_text = "\n".join(detected_texts)
|
| 100 |
+
|
| 101 |
+
return output_path, full_text
|
| 102 |
+
except Exception as e:
|
| 103 |
+
return None, f"Error processing video: {str(e)}"
|
| 104 |
+
|
| 105 |
+
# Create Gradio interface
|
| 106 |
+
iface = gr.Interface(
|
| 107 |
+
fn=process_video,
|
| 108 |
+
inputs=gr.Video(), # Input video file
|
| 109 |
+
outputs=[gr.Video(), gr.Textbox()] # Output video file and detected text
|
| 110 |
+
)
|
| 111 |
+
|
| 112 |
+
# Launch the interface
|
| 113 |
+
iface.launch()
|