Delete app-video.py
Browse files- app-video.py +0 -142
app-video.py
DELETED
@@ -1,142 +0,0 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
from dotenv import load_dotenv
|
3 |
-
from roboflow import Roboflow
|
4 |
-
import tempfile
|
5 |
-
import os
|
6 |
-
import requests
|
7 |
-
import cv2
|
8 |
-
|
9 |
-
# Muat variabel lingkungan dari file .env
|
10 |
-
load_dotenv()
|
11 |
-
api_key = os.getenv("ROBOFLOW_API_KEY")
|
12 |
-
workspace = os.getenv("ROBOFLOW_WORKSPACE")
|
13 |
-
project_name = os.getenv("ROBOFLOW_PROJECT")
|
14 |
-
model_version = int(os.getenv("ROBOFLOW_MODEL_VERSION"))
|
15 |
-
|
16 |
-
# Inisialisasi Roboflow menggunakan data yang diambil dari secrets
|
17 |
-
rf = Roboflow(api_key=api_key)
|
18 |
-
project = rf.workspace(workspace).project(project_name)
|
19 |
-
model = project.version(model_version).model
|
20 |
-
|
21 |
-
# Fungsi untuk menangani deteksi pada gambar
|
22 |
-
def detect_objects(image):
|
23 |
-
# Simpan gambar yang diupload sebagai file sementara
|
24 |
-
with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as temp_file:
|
25 |
-
image.save(temp_file, format="JPEG")
|
26 |
-
temp_file_path = temp_file.name
|
27 |
-
|
28 |
-
try:
|
29 |
-
# Lakukan prediksi pada gambar
|
30 |
-
predictions = model.predict(temp_file_path, confidence=60, overlap=80).json()
|
31 |
-
|
32 |
-
# Menghitung jumlah objek per kelas
|
33 |
-
class_count = {}
|
34 |
-
total_count = 0
|
35 |
-
|
36 |
-
for prediction in predictions['predictions']:
|
37 |
-
class_name = prediction['class']
|
38 |
-
class_count[class_name] = class_count.get(class_name, 0) + 1
|
39 |
-
total_count += 1
|
40 |
-
|
41 |
-
# Menyusun output berupa string hasil perhitungan
|
42 |
-
result_text = "Product Nestle\n\n"
|
43 |
-
for class_name, count in class_count.items():
|
44 |
-
result_text += f"{class_name}: {count}\n"
|
45 |
-
result_text += f"\nTotal Product Nestle: {total_count}"
|
46 |
-
|
47 |
-
# Menyimpan gambar dengan prediksi
|
48 |
-
output_image_path = "/tmp/prediction.jpg"
|
49 |
-
model.predict(temp_file_path, confidence=60, overlap=80).save(output_image_path)
|
50 |
-
|
51 |
-
except requests.exceptions.HTTPError as http_err:
|
52 |
-
result_text = f"HTTP error occurred: {http_err}"
|
53 |
-
output_image_path = temp_file_path
|
54 |
-
except Exception as err:
|
55 |
-
result_text = f"An error occurred: {err}"
|
56 |
-
output_image_path = temp_file_path
|
57 |
-
|
58 |
-
os.remove(temp_file_path)
|
59 |
-
|
60 |
-
return output_image_path, result_text
|
61 |
-
|
62 |
-
# Fungsi untuk menangani deteksi pada video
|
63 |
-
def detect_objects_in_video(video_path):
|
64 |
-
temp_output_path = "/tmp/output_video.mp4"
|
65 |
-
temp_frames_dir = tempfile.mkdtemp()
|
66 |
-
|
67 |
-
try:
|
68 |
-
# Baca video dan ekstrak frame
|
69 |
-
video = cv2.VideoCapture(video_path)
|
70 |
-
frame_rate = int(video.get(cv2.CAP_PROP_FPS))
|
71 |
-
frame_width = int(video.get(cv2.CAP_PROP_FRAME_WIDTH))
|
72 |
-
frame_height = int(video.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
73 |
-
frame_size = (frame_width, frame_height)
|
74 |
-
frame_count = int(video.get(cv2.CAP_PROP_FRAME_COUNT))
|
75 |
-
|
76 |
-
# VideoWriter untuk membuat video keluaran
|
77 |
-
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
78 |
-
output_video = cv2.VideoWriter(temp_output_path, fourcc, frame_rate, frame_size)
|
79 |
-
|
80 |
-
frame_index = 0
|
81 |
-
while True:
|
82 |
-
ret, frame = video.read()
|
83 |
-
if not ret:
|
84 |
-
break
|
85 |
-
|
86 |
-
# Simpan frame sementara untuk prediksi
|
87 |
-
frame_path = os.path.join(temp_frames_dir, f"frame_{frame_index}.jpg")
|
88 |
-
cv2.imwrite(frame_path, frame)
|
89 |
-
|
90 |
-
# Deteksi objek pada frame
|
91 |
-
predictions = model.predict(frame_path, confidence=60, overlap=80).json()
|
92 |
-
|
93 |
-
# Gambar bounding box pada frame
|
94 |
-
for prediction in predictions['predictions']:
|
95 |
-
x, y, w, h = prediction['x'], prediction['y'], prediction['width'], prediction['height']
|
96 |
-
class_name = prediction['class']
|
97 |
-
color = (0, 255, 0) # Hijau
|
98 |
-
cv2.rectangle(frame, (int(x - w/2), int(y - h/2)), (int(x + w/2), int(y + h/2)), color, 2)
|
99 |
-
cv2.putText(frame, class_name, (int(x - w/2), int(y - h/2 - 10)), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
|
100 |
-
|
101 |
-
# Tambahkan frame ke video keluaran
|
102 |
-
output_video.write(frame)
|
103 |
-
frame_index += 1
|
104 |
-
|
105 |
-
video.release()
|
106 |
-
output_video.release()
|
107 |
-
|
108 |
-
return temp_output_path, "Video processing completed successfully."
|
109 |
-
|
110 |
-
except Exception as e:
|
111 |
-
return None, f"An error occurred: {e}"
|
112 |
-
|
113 |
-
# Membuat antarmuka Gradio dengan tata letak fleksibel
|
114 |
-
with gr.Blocks() as iface:
|
115 |
-
with gr.Row():
|
116 |
-
with gr.Column():
|
117 |
-
input_image = gr.Image(type="pil", label="Input Image")
|
118 |
-
input_video = gr.Video(label="Input Video") # Updated line
|
119 |
-
with gr.Column():
|
120 |
-
output_image = gr.Image(label="Detect Object")
|
121 |
-
output_video = gr.Video(label="Output Video")
|
122 |
-
with gr.Column():
|
123 |
-
output_text = gr.Textbox(label="Counting Object")
|
124 |
-
|
125 |
-
# Tombol untuk memproses gambar
|
126 |
-
detect_image_button = gr.Button("Detect Image")
|
127 |
-
detect_image_button.click(
|
128 |
-
fn=detect_objects,
|
129 |
-
inputs=input_image,
|
130 |
-
outputs=[output_image, output_text]
|
131 |
-
)
|
132 |
-
|
133 |
-
# Tombol untuk memproses video
|
134 |
-
detect_video_button = gr.Button("Detect Video")
|
135 |
-
detect_video_button.click(
|
136 |
-
fn=detect_objects_in_video,
|
137 |
-
inputs=input_video,
|
138 |
-
outputs=[output_video, output_text]
|
139 |
-
)
|
140 |
-
|
141 |
-
# Menjalankan antarmuka
|
142 |
-
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|