xgenatiik commited on
Commit
bdc6c71
·
verified ·
1 Parent(s): 532552a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -48
app.py CHANGED
@@ -1,48 +1,48 @@
1
- import gradio as gr
2
- from ultralytics import YOLO
3
- from PIL import Image
4
- import numpy as np
5
-
6
- # Load YOLO model
7
- model = YOLO("best.pt")
8
-
9
- # Define the prediction function
10
- def detect_species(files):
11
- """
12
- Detect species in uploaded images using the YOLO model.
13
-
14
- Args:
15
- files (list): List of uploaded image files.
16
-
17
- Returns:
18
- list: Annotated images or error messages for each file.
19
- """
20
- annotated_images = []
21
- for file in files: # Loop through the list of uploaded files
22
- try:
23
- # Open and process the image
24
- image = Image.open(file)
25
- image = np.array(image) # Convert to numpy array
26
- results = model(image) # Run the YOLO model
27
- annotated_image = results[0].plot() # Generate annotated image
28
- annotated_images.append(annotated_image) # Add to results
29
- except Exception as e:
30
- # Handle errors (e.g., unsupported or corrupt files)
31
- error_message = f"Error processing file {file.name}: {str(e)}"
32
- print(error_message) # Log the error
33
- annotated_images.append(np.zeros((100, 100, 3))) # Placeholder image
34
- return annotated_images
35
-
36
- # Create the Gradio app
37
- app = gr.Interface(
38
- fn=detect_species,
39
- inputs=gr.Files(file_types=["image"], label="Upload Images"), # Allow multiple image uploads
40
- outputs=gr.Gallery(label="Detection Results"), # Display results in a gallery
41
- title="Plant Species Detector",
42
- description="Upload one or more leaf images, and the model will detect the species.",
43
- examples=["example1.jpg", "example2.jpg"], # Optional: Add example images
44
- )
45
-
46
- # Launch the app
47
- if __name__ == "__main__":
48
- app.launch(share=True) # Set share=True to get a public URL
 
1
+ import gradio as gr
2
+ from ultralytics import YOLO
3
+ from PIL import Image
4
+ import numpy as np
5
+
6
+ # Load YOLO model
7
+ model = YOLO("best.pt")
8
+
9
+ # Define the prediction function
10
+ def detect_species(files):
11
+ """
12
+ Detect species in uploaded images using the YOLO model.
13
+
14
+ Args:
15
+ files (list): List of uploaded image files.
16
+
17
+ Returns:
18
+ list: Annotated images or error messages for each file.
19
+ """
20
+ annotated_images = []
21
+ for file in files: # Loop through the list of uploaded files
22
+ try:
23
+ # Open and process the image
24
+ image = Image.open(file)
25
+ image = np.array(image) # Convert to numpy array
26
+ results = model(image) # Run the YOLO model
27
+ annotated_image = results[0].plot() # Generate annotated image
28
+ annotated_images.append(annotated_image) # Add to results
29
+ except Exception as e:
30
+ # Handle errors (e.g., unsupported or corrupt files)
31
+ error_message = f"Error processing file {file.name}: {str(e)}"
32
+ print(error_message) # Log the error
33
+ annotated_images.append(np.zeros((100, 100, 3))) # Placeholder image
34
+ return annotated_images
35
+
36
+ # Create the Gradio app
37
+ app = gr.Interface(
38
+ fn=detect_species,
39
+ inputs=gr.Files(file_types=["image"], label="Upload Images"), # Allow multiple image uploads
40
+ outputs=gr.Gallery(label="Detection Results"), # Display results in a gallery
41
+ title="Finnish Meadow Plants Detection",
42
+ description="Upload one or more leaf images, and the model will detect the species.",
43
+ examples=["example1.jpg", "example2.jpg"], # Optional: Add example images
44
+ )
45
+
46
+ # Launch the app
47
+ if __name__ == "__main__":
48
+ app.launch(share=True) # Set share=True to get a public URL