Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +103 -0
- requirements.txt +5 -0
app.py
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
import cv2
|
| 4 |
+
import os
|
| 5 |
+
import torch.nn as nn
|
| 6 |
+
import numpy as np
|
| 7 |
+
from torchvision.ops import box_iou
|
| 8 |
+
from PIL import Image
|
| 9 |
+
import albumentations as A
|
| 10 |
+
from albumentations.pytorch import ToTensorV2
|
| 11 |
+
|
| 12 |
+
from timeit import default_timer as timer
|
| 13 |
+
from typing import Tuple, Dict
|
| 14 |
+
|
| 15 |
+
# apply nms algorithm
|
| 16 |
+
def apply_nms(orig_prediction, iou_thresh=0.3):
|
| 17 |
+
# torchvision returns the indices of the bboxes to keep
|
| 18 |
+
keep = torchvision.ops.nms(orig_prediction['boxes'], orig_prediction['scores'], iou_thresh)
|
| 19 |
+
final_prediction = orig_prediction
|
| 20 |
+
final_prediction['boxes'] = final_prediction['boxes'][keep]
|
| 21 |
+
final_prediction['scores'] = final_prediction['scores'][keep]
|
| 22 |
+
final_prediction['labels'] = final_prediction['labels'][keep]
|
| 23 |
+
|
| 24 |
+
return final_prediction
|
| 25 |
+
|
| 26 |
+
# Draw the bounding box
|
| 27 |
+
def plot_img_bbox(img, target):
|
| 28 |
+
for box in (target['boxes']):
|
| 29 |
+
xmin, ymin, xmax, ymax = int(box[0].cpu()), int(box[1].cpu()), int(box[2].cpu()),int(box[3].cpu())
|
| 30 |
+
cv2.rectangle(img, (xmin, ymin), (xmax, ymax), (0, 0, 255), 2)
|
| 31 |
+
label = "palm"
|
| 32 |
+
# Add the label and confidence score
|
| 33 |
+
label = f'{label}'
|
| 34 |
+
cv2.putText(img, label, (xmin, ymin - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 0, 255), 2)
|
| 35 |
+
|
| 36 |
+
# Display the image with detections
|
| 37 |
+
filename = 'pred.jpg'
|
| 38 |
+
cv2.imwrite(filename, img)
|
| 39 |
+
|
| 40 |
+
# transform image
|
| 41 |
+
test_transforms = A.Compose([
|
| 42 |
+
A.Resize(height=1024, width=1024, always_apply=True),
|
| 43 |
+
A.Normalize(always_apply=True),
|
| 44 |
+
ToTensorV2(always_apply=True),])
|
| 45 |
+
|
| 46 |
+
# select device (whether GPU or CPU)
|
| 47 |
+
device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')
|
| 48 |
+
|
| 49 |
+
# model loading
|
| 50 |
+
model = torch.load('pickel.pth',map_location=torch.device('cpu'))
|
| 51 |
+
model = model.to(device)
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
def predict(img) -> Tuple[Dict, float]:
|
| 55 |
+
|
| 56 |
+
# Start a timer
|
| 57 |
+
start_time = timer()
|
| 58 |
+
|
| 59 |
+
# Transform the target image and add a batch dimension
|
| 60 |
+
image_transformed = test_transforms(np.array(img))
|
| 61 |
+
|
| 62 |
+
image_transformed = image_transformed.unsqueeze(0)
|
| 63 |
+
image_transformed = image_transformed.to(device)
|
| 64 |
+
|
| 65 |
+
# inference
|
| 66 |
+
model.eval()
|
| 67 |
+
with torch.no_grad():
|
| 68 |
+
predictions = model(image_transformed)[0]
|
| 69 |
+
|
| 70 |
+
nms_prediction = apply_nms(predictions, iou_thresh=0.1)
|
| 71 |
+
|
| 72 |
+
plot_img_bbox(img, nms_prediction)
|
| 73 |
+
|
| 74 |
+
pred = np.array(Image.open("pred.jpg"))
|
| 75 |
+
|
| 76 |
+
# Calculate the prediction time
|
| 77 |
+
pred_time = round(timer() - start_time, 5)
|
| 78 |
+
|
| 79 |
+
# Return the prediction dictionary and prediction time
|
| 80 |
+
return pred,pred_time
|
| 81 |
+
|
| 82 |
+
### 4. Gradio app ###
|
| 83 |
+
# Create title, description and article strings
|
| 84 |
+
title = "🌴Palm trees detection🌴"
|
| 85 |
+
description = "Faster r-cnn model to detect oil palm trees in drones images."
|
| 86 |
+
article = "Created by data354."
|
| 87 |
+
|
| 88 |
+
# Create examples list from "examples/" directory
|
| 89 |
+
example_list = [["examples/" + example] for example in os.listdir("examples")]
|
| 90 |
+
|
| 91 |
+
# Create the Gradio demo
|
| 92 |
+
demo = gr.Interface(fn=predict, # mapping function from input to output
|
| 93 |
+
inputs=gr.Image(type="pil"), # what are the inputs?
|
| 94 |
+
outputs=[gr.Label(label="Predictions"), # what are the outputs?
|
| 95 |
+
gr.Number(label="Prediction time (s)")], # our fn has two outputs, therefore we have two outputs
|
| 96 |
+
# Create examples list from "examples/" directory
|
| 97 |
+
examples=example_list,
|
| 98 |
+
title=title,
|
| 99 |
+
description=description,
|
| 100 |
+
article=article
|
| 101 |
+
)
|
| 102 |
+
# Launch the demo!
|
| 103 |
+
demo.launch(debug = False)
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch
|
| 2 |
+
torchvision
|
| 3 |
+
opencv-python
|
| 4 |
+
numpy
|
| 5 |
+
albumentations
|