Priyanka-Kumavat commited on
Commit
3b14887
·
1 Parent(s): 01b0ba8

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +138 -0
app.py ADDED
@@ -0,0 +1,138 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import torch
3
+ import torchvision
4
+ import torchvision.transforms as transforms
5
+ from torchvision.models.detection.faster_rcnn import FastRCNNPredictor
6
+ from torchvision.transforms import ToTensor
7
+ from PIL import Image, ImageDraw
8
+ import cv2
9
+ import numpy as np
10
+ import pandas as pd
11
+ import os
12
+
13
+ import tempfile
14
+ from tempfile import NamedTemporaryFile
15
+
16
+ # Create an FRCNN model instance with the same structure as the saved model
17
+ model = torchvision.models.detection.fasterrcnn_resnet50_fpn(num_classes=91)
18
+
19
+ # Load the saved parameters into the model
20
+ model.load_state_dict(torch.load("frcnn_model.pth"))
21
+
22
+ # Define the classes for object detection
23
+ classes = [
24
+ '__background__', 'person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus',
25
+ 'train', 'truck', 'boat', 'traffic light', 'fire hydrant', 'N/A', 'stop sign',
26
+ 'parking meter', 'bench', 'bird', 'cat', 'dog', 'horse', 'sheep', 'cow',
27
+ 'elephant', 'bear', 'zebra', 'giraffe', 'N/A', 'backpack', 'umbrella', 'N/A',
28
+ 'N/A', 'handbag', 'tie', 'suitcase', 'frisbee', 'skis', 'snowboard',
29
+ 'sports ball', 'kite', 'baseball bat', 'baseball glove', 'skateboard',
30
+ 'surfboard', 'tennis racket', 'bottle', 'N/A', 'wine glass', 'cup', 'fork',
31
+ 'knife', 'spoon', 'bowl', 'banana', 'apple', 'sandwich', 'orange',
32
+ 'broccoli', 'carrot', 'hot dog', 'pizza', 'donut', 'cake', 'chair', 'couch',
33
+ 'potted plant', 'bed', 'N/A', 'dining table', 'N/A', 'N/A', 'toilet', 'N/A',
34
+ 'tv', 'laptop', 'mouse', 'remote', 'keyboard', 'cell phone', 'microwave',
35
+ 'oven', 'toaster', 'sink', 'refrigerator', 'N/A', 'book', 'clock', 'vase',
36
+ 'scissors', 'teddy bear', 'hair drier', 'toothbrush'
37
+ ]
38
+
39
+ # Set the threshold for object detection. It is IoU (Intersection over Union)
40
+ threshold = 0.5
41
+
42
+ st.title(""" Image Object Detections """)
43
+
44
+ # st.subheader("Prediction of Object Detection")
45
+
46
+ st.write(""" The Faster R-CNN (Region-based Convolutional Neural Network) is a cutting-edge object detection model that combines deep
47
+ learning with region proposal networks to achieve highly accurate object detection in images.
48
+ It is trained on a large dataset of images and can detect a wide range of objects with high precision and recall.
49
+ The model is based on the ResNet-50 architecture, which allows it to capture complex visual features from the input image.
50
+ It uses a two-stage approach, first proposing regions of interest (RoIs) in the image and then classifying and refining the
51
+ object boundaries within these RoIs. This approach makes it extremely efficient and accurate in detecting multiple objects
52
+ in a single image.
53
+ """)
54
+
55
+
56
+ # images = ["test2.jpg","img7.jpg","img20.jpg","img23.jpg"]
57
+ # with st.sidebar:
58
+ # st.write("Choose an image")
59
+ # selected_image = st.selectbox("Select an image", images)
60
+
61
+
62
+ images = ["test2.jpg","img7.jpg","img20.jpg","img23.jpg"]
63
+ with st.sidebar:
64
+ st.write("Choose an image")
65
+ st.image(images)
66
+
67
+
68
+ # define the function to perform object detection on an image
69
+ def detect_objects(image_path):
70
+ # load the image
71
+ image = Image.open(image_path).convert('RGB')
72
+
73
+ # convert the image to a tensor
74
+ image_tensor = ToTensor()(image).to(device)
75
+
76
+ # run the image through the model to get the predictions
77
+ model.eval()
78
+ with torch.no_grad():
79
+ predictions = model([image_tensor])
80
+
81
+ # filter out the predictions below the threshold
82
+ scores = predictions[0]['scores'].cpu().numpy()
83
+ boxes = predictions[0]['boxes'].cpu().numpy()
84
+ labels = predictions[0]['labels'].cpu().numpy()
85
+ mask = scores > threshold
86
+ scores = scores[mask]
87
+ boxes = boxes[mask]
88
+ labels = labels[mask]
89
+
90
+ # create a new image with the predicted objects outlined in rectangles
91
+ draw = ImageDraw.Draw(image)
92
+ for box, label in zip(boxes, labels):
93
+
94
+ # draw the rectangle around the object
95
+ draw.rectangle([(box[0], box[1]), (box[2], box[3])], outline='red')
96
+
97
+ # write the object class above the rectangle
98
+ class_name = classes[label]
99
+ draw.text((box[0], box[1]), class_name, fill='yellow')
100
+
101
+ # show the image
102
+ st.write("Obects detected in the image are: ")
103
+ # st.image(image, use_column_width=True)
104
+ st.image.show()
105
+
106
+
107
+ file = st.file_uploader('Upload an Image', type=(["jpeg", "jpg", "png"]))
108
+
109
+ if file is None:
110
+ st.write("Please upload an image file")
111
+ else:
112
+ image = Image.open(file)
113
+ st.write("Input Image")
114
+ st.image(image, use_column_width=True)
115
+ with NamedTemporaryFile(dir='.', suffix='.jpeg') as f: # this line gives error and only accepts .jpeg and so used above snippet
116
+ f.write(file.getbuffer()) # which will accepts all formats of images.
117
+ # your_function_which_takes_a_path(f.name)
118
+ detect_objects(f.name)
119
+
120
+ # if file is None:
121
+ # st.write("Please upload an image file")
122
+ # else:
123
+ # image = Image.open(file)
124
+ # st.write("Input Image")
125
+ # st.image(image, use_column_width=True)
126
+ # with NamedTemporaryFile(dir='.', suffix='.' + file.name.split('.')[-1]) as f:
127
+ # f.write(file.getbuffer())
128
+ # # your_function_which_takes_a_path(f.name)
129
+ # detect_objects(f.name)
130
+
131
+
132
+ st.write(""" This Streamlit app provides a user-friendly interface for uploading an image and visualizing the output of the Faster R-CNN
133
+ model. It displays the uploaded image along with the predicted objects highlighted with bounding box overlays. The app allows
134
+ users to explore the detected objects in the image, providing valuable insights and understanding of the model's predictions.
135
+ It can be used for a wide range of applications, such as object recognition, image analysis, and visual storytelling.
136
+ Whether it's identifying objects in real-world images or understanding the capabilities of state-of-the-art object detection
137
+ models, this Streamlit app powered by Faster R-CNN is a powerful tool for computer vision tasks.
138
+ """)