Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,36 +1,34 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import os
|
| 3 |
-
import tempfile
|
| 4 |
-
from roboflow import Roboflow
|
| 5 |
from dotenv import load_dotenv
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
#
|
| 8 |
load_dotenv()
|
| 9 |
-
|
| 10 |
-
# Ambil nilai dari environment variables
|
| 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 |
-
#
|
| 17 |
rf = Roboflow(api_key=api_key)
|
| 18 |
project = rf.workspace(workspace).project(project_name)
|
| 19 |
model = project.version(model_version).model
|
| 20 |
|
| 21 |
-
#
|
| 22 |
def detect_objects(image):
|
| 23 |
-
#
|
| 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 |
-
#
|
| 29 |
predictions = model.predict(temp_file_path, confidence=60, overlap=80).json()
|
| 30 |
|
| 31 |
-
#
|
| 32 |
class_count = {}
|
| 33 |
-
total_count = 0 #
|
| 34 |
|
| 35 |
for prediction in predictions['predictions']:
|
| 36 |
class_name = prediction['class']
|
|
@@ -38,40 +36,39 @@ def detect_objects(image):
|
|
| 38 |
class_count[class_name] += 1
|
| 39 |
else:
|
| 40 |
class_count[class_name] = 1
|
| 41 |
-
total_count += 1 #
|
| 42 |
|
| 43 |
-
#
|
| 44 |
-
result_text = "Product Nestle\n\n"
|
|
|
|
| 45 |
for class_name, count in class_count.items():
|
| 46 |
result_text += f"{class_name}: {count} \n"
|
| 47 |
|
| 48 |
-
result_text += f"\nTotal Product Nestle: {total_count}"
|
| 49 |
|
| 50 |
-
#
|
| 51 |
-
|
|
|
|
| 52 |
|
| 53 |
-
#
|
| 54 |
os.remove(temp_file_path)
|
| 55 |
|
| 56 |
-
return
|
| 57 |
-
|
| 58 |
-
# Membuat antarmuka Gradio dengan label yang telah diganti
|
| 59 |
-
inputs = gr.Image(type="pil", label="Input Image") # Label input
|
| 60 |
-
outputs = [gr.Image(label="Detect Object"), gr.Textbox(label="Counting Object")] # Label output
|
| 61 |
-
|
| 62 |
-
# Membuat layout dengan gr.Row() untuk menampilkan input dan output berdampingan
|
| 63 |
-
iface = gr.Interface(
|
| 64 |
-
fn=detect_objects,
|
| 65 |
-
inputs=inputs,
|
| 66 |
-
outputs=outputs,
|
| 67 |
-
live=True
|
| 68 |
-
)
|
| 69 |
|
| 70 |
-
#
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
-
#
|
| 77 |
iface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
| 2 |
from dotenv import load_dotenv
|
| 3 |
+
from roboflow import Roboflow
|
| 4 |
+
import tempfile
|
| 5 |
+
import os
|
| 6 |
|
| 7 |
+
# Load environment variables from .env file
|
| 8 |
load_dotenv()
|
|
|
|
|
|
|
| 9 |
api_key = os.getenv("ROBOFLOW_API_KEY")
|
| 10 |
workspace = os.getenv("ROBOFLOW_WORKSPACE")
|
| 11 |
project_name = os.getenv("ROBOFLOW_PROJECT")
|
| 12 |
model_version = int(os.getenv("ROBOFLOW_MODEL_VERSION"))
|
| 13 |
|
| 14 |
+
# Initialize Roboflow using the loaded environment variables
|
| 15 |
rf = Roboflow(api_key=api_key)
|
| 16 |
project = rf.workspace(workspace).project(project_name)
|
| 17 |
model = project.version(model_version).model
|
| 18 |
|
| 19 |
+
# Function to handle image input and output
|
| 20 |
def detect_objects(image):
|
| 21 |
+
# Save the uploaded image as a temporary file
|
| 22 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as temp_file:
|
| 23 |
image.save(temp_file, format="JPEG")
|
| 24 |
temp_file_path = temp_file.name
|
| 25 |
|
| 26 |
+
# Perform prediction on the image
|
| 27 |
predictions = model.predict(temp_file_path, confidence=60, overlap=80).json()
|
| 28 |
|
| 29 |
+
# Count the number of objects per class
|
| 30 |
class_count = {}
|
| 31 |
+
total_count = 0 # Store the total number of objects
|
| 32 |
|
| 33 |
for prediction in predictions['predictions']:
|
| 34 |
class_name = prediction['class']
|
|
|
|
| 36 |
class_count[class_name] += 1
|
| 37 |
else:
|
| 38 |
class_count[class_name] = 1
|
| 39 |
+
total_count += 1 # Increment total object count for each prediction
|
| 40 |
|
| 41 |
+
# Prepare the result text
|
| 42 |
+
result_text = "Product Nestle\n\n"
|
| 43 |
+
|
| 44 |
for class_name, count in class_count.items():
|
| 45 |
result_text += f"{class_name}: {count} \n"
|
| 46 |
|
| 47 |
+
result_text += f"\nTotal Product Nestle: {total_count}"
|
| 48 |
|
| 49 |
+
# Save the image with predictions
|
| 50 |
+
output_image_path = "/tmp/prediction.jpg"
|
| 51 |
+
model.predict(temp_file_path, confidence=60, overlap=80).save(output_image_path)
|
| 52 |
|
| 53 |
+
# Remove the temporary file after prediction
|
| 54 |
os.remove(temp_file_path)
|
| 55 |
|
| 56 |
+
return output_image_path, result_text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
+
# Create the Gradio interface
|
| 59 |
+
with gr.Blocks() as iface:
|
| 60 |
+
with gr.Row():
|
| 61 |
+
image_input = gr.Image(type="pil", label="Input Image")
|
| 62 |
+
with gr.Row():
|
| 63 |
+
with gr.Column():
|
| 64 |
+
image_output = gr.Image(label="Detect Object")
|
| 65 |
+
with gr.Column():
|
| 66 |
+
text_output = gr.Textbox(label="Counting Object")
|
| 67 |
+
gr.Interface(
|
| 68 |
+
fn=detect_objects,
|
| 69 |
+
inputs=image_input,
|
| 70 |
+
outputs=[image_output, text_output],
|
| 71 |
+
)
|
| 72 |
|
| 73 |
+
# Launch the interface
|
| 74 |
iface.launch()
|