Yudsky commited on
Commit
c84a366
1 Parent(s): 3030cd6

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -0
app.py ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from ultralytics import YOLO
3
+ from PIL import Image
4
+ import requests
5
+
6
+ # Periksa apakah GPU tersedia dan pilih perangkat GPU jika ada
7
+ device = 'cuda' if torch.cuda.is_available() else 'cpu'
8
+
9
+ # Step 1: Download the YOLO model from Hugging Face
10
+ url = 'https://huggingface.co/Yudsky/pest-detection-yolo11/resolve/main/best.pt'
11
+ model_path = 'best.pt'
12
+
13
+ # Check if the model file already exists, if not, download it
14
+ if not os.path.exists(model_path):
15
+ print("Downloading the model...")
16
+ response = requests.get(url)
17
+ with open(model_path, 'wb') as f:
18
+ f.write(response.content)
19
+ print("Download completed.")
20
+
21
+ # Step 2: Load the model using YOLO from ultralytics
22
+ # print("Loading the model...")
23
+ model = YOLO(model_path)
24
+ # print("Model loaded successfully.")
25
+
26
+ # Step 3: Define the prediction functions for images and videos
27
+ def predict_image(image):
28
+ # Run inference
29
+ results = model(image, device=device)
30
+ # Plot results on the image
31
+ annotated_image = results[0].plot() # Get the annotated image with bounding boxes
32
+ return Image.fromarray(annotated_image)
33
+
34
+ inputs_image = [
35
+ gr.Image(type='filepath', label='input image')
36
+ ]
37
+ outputs_image = [
38
+ gr.Image(type='numpy', label='output image')
39
+ ]
40
+
41
+ # Step 4: Define the Gradio Interface
42
+ interface_image = gr.Interface(
43
+ fn=predict_image,
44
+ inputs=inputs_image,
45
+ outputs=outputs_image,
46
+ title="Pest Detection",
47
+ description="Upload an image and the model will detect pests."
48
+ )
49
+
50
+ # Step 5: Launch the interface
51
+ interface_image.launch()