hafizarslan commited on
Commit
81f02dd
·
verified ·
1 Parent(s): 66f3bfc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +86 -0
app.py CHANGED
@@ -0,0 +1,86 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ import torch
3
+ import numpy as np
4
+ from PIL import Image
5
+ from torchvision import models, transforms
6
+ from ultralytics import YOLO
7
+ import gradio as gr
8
+ import torch.nn as nn
9
+
10
+ # Initialize device
11
+ device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
12
+
13
+ # Load models
14
+ yolo_model = YOLO('best.pt') # Make sure this file is uploaded to your Space
15
+ resnet = models.resnet50(pretrained=False)
16
+
17
+ # Modify ResNet for 3 classes
18
+ resnet.fc = nn.Linear(resnet.fc.in_features, 3)
19
+ resnet.load_state_dict(torch.load('rice_resnet_model.pth', map_location=device))
20
+ resnet = resnet.to(device)
21
+ resnet.eval()
22
+
23
+ # Class labels
24
+ class_labels = ["c9", "kant", "superf"]
25
+
26
+ # Image transformations
27
+ transform = transforms.Compose([
28
+ transforms.Resize((224, 224)),
29
+ transforms.ToTensor(),
30
+ transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
31
+ ])
32
+
33
+ def classify_crop(crop_img):
34
+ """Classify a single rice grain"""
35
+ image = transform(crop_img).unsqueeze(0).to(device)
36
+ with torch.no_grad():
37
+ output = resnet(image)
38
+ _, predicted = torch.max(output, 1)
39
+ return class_labels[predicted.item()]
40
+
41
+ def detect_and_classify(image):
42
+ """Process full image with YOLO + ResNet"""
43
+ image = np.array(image)
44
+ results = yolo_model(image)[0]
45
+ boxes = results.boxes.xyxy.cpu().numpy()
46
+
47
+ for box in boxes:
48
+ x1, y1, x2, y2 = map(int, box[:4])
49
+ crop = image[y1:y2, x1:x2]
50
+ crop_pil = Image.fromarray(cv2.cvtColor(crop, cv2.COLOR_BGR2RGB))
51
+ predicted_label = classify_crop(crop_pil)
52
+
53
+ # Draw bounding box and label
54
+ cv2.rectangle(image, (x1, y1), (x2, y2), (0, 255, 0), 2)
55
+ cv2.putText(image, predicted_label, (x1, y1-10),
56
+ cv2.FONT_HERSHEY_SIMPLEX, 0.9, (36, 255, 12), 2)
57
+
58
+ return Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
59
+
60
+ # Gradio Interface
61
+ with gr.Blocks(title="چاول کا شناختی نظام") as demo:
62
+ gr.Markdown("""
63
+ # چاول کا شناختی نظام
64
+ ایک تصویر اپ لوڈ کریں جس میں چاول کے دانے ہوں۔ نظام ہر دانے کو پہچان کر اس کی قسم بتائے گا۔
65
+ """)
66
+
67
+ with gr.Row():
68
+ input_image = gr.Image(type="pil", label="تصویر داخل کریں")
69
+ output_image = gr.Image(type="pil", label="نتیجہ")
70
+
71
+ submit_btn = gr.Button("تشخیص کریں")
72
+ submit_btn.click(
73
+ fn=detect_and_classify,
74
+ inputs=input_image,
75
+ outputs=output_image
76
+ )
77
+
78
+ gr.Examples(
79
+ examples=[["example1.jpg"], ["example2.jpg"]], # Add your example images
80
+ inputs=input_image,
81
+ outputs=output_image,
82
+ fn=detect_and_classify,
83
+ cache_examples=True
84
+ )
85
+
86
+ demo.launch()