muhammadsalmanalfaridzi commited on
Commit
76d7581
·
verified ·
1 Parent(s): 1b69462

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +73 -16
app.py CHANGED
@@ -4,6 +4,7 @@ from roboflow import Roboflow
4
  import tempfile
5
  import os
6
  import requests
 
7
 
8
  # Muat variabel lingkungan dari file .env
9
  load_dotenv()
@@ -17,7 +18,7 @@ rf = Roboflow(api_key=api_key)
17
  project = rf.workspace(workspace).project(project_name)
18
  model = project.version(model_version).model
19
 
20
- # Fungsi untuk menangani input dan output gambar
21
  def detect_objects(image):
22
  # Simpan gambar yang diupload sebagai file sementara
23
  with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as temp_file:
@@ -26,16 +27,16 @@ def detect_objects(image):
26
 
27
  try:
28
  # Lakukan prediksi pada gambar
29
- predictions = model.predict(temp_file_path, confidence=50, overlap=80).json()
30
 
31
  # Menghitung jumlah objek per kelas
32
  class_count = {}
33
- total_count = 0 # Menyimpan total jumlah objek
34
 
35
  for prediction in predictions['predictions']:
36
  class_name = prediction['class']
37
  class_count[class_name] = class_count.get(class_name, 0) + 1
38
- total_count += 1 # Tambah jumlah objek untuk setiap prediksi
39
 
40
  # Menyusun output berupa string hasil perhitungan
41
  result_text = "Product Nestle\n\n"
@@ -45,41 +46,97 @@ def detect_objects(image):
45
 
46
  # Menyimpan gambar dengan prediksi
47
  output_image_path = "/tmp/prediction.jpg"
48
- model.predict(temp_file_path, confidence=50, overlap=80).save(output_image_path)
49
 
50
  except requests.exceptions.HTTPError as http_err:
51
- # Menangani kesalahan HTTP
52
  result_text = f"HTTP error occurred: {http_err}"
53
- output_image_path = temp_file_path # Kembalikan gambar asli jika terjadi error
54
  except Exception as err:
55
- # Menangani kesalahan lain
56
  result_text = f"An error occurred: {err}"
57
- output_image_path = temp_file_path # Kembalikan gambar asli jika terjadi error
58
 
59
- # Hapus file sementara setelah prediksi
60
  os.remove(temp_file_path)
61
 
62
  return output_image_path, result_text
63
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64
  # Membuat antarmuka Gradio dengan tata letak fleksibel
65
  with gr.Blocks() as iface:
66
  with gr.Row():
67
  with gr.Column():
68
  input_image = gr.Image(type="pil", label="Input Image")
 
69
  with gr.Column():
70
  output_image = gr.Image(label="Detect Object")
 
71
  with gr.Column():
72
  output_text = gr.Textbox(label="Counting Object")
73
 
74
- # Tombol untuk memproses input
75
- detect_button = gr.Button("Detect")
76
-
77
- # Hubungkan tombol dengan fungsi deteksi
78
- detect_button.click(
79
  fn=detect_objects,
80
  inputs=input_image,
81
  outputs=[output_image, output_text]
82
  )
83
 
 
 
 
 
 
 
 
 
84
  # Menjalankan antarmuka
85
- iface.launch()
 
4
  import tempfile
5
  import os
6
  import requests
7
+ import cv2
8
 
9
  # Muat variabel lingkungan dari file .env
10
  load_dotenv()
 
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:
 
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"
 
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(type="file", label="Input Video")
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()