Skip to content

Commit f2717ca

Browse files
Add files via upload
1 parent 70afcb3 commit f2717ca

9 files changed

+464
-63
lines changed

Diff for: Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ RUN apt-get update && apt-get install -y mesa-utils
1515

1616
RUN pip install --upgrade pip && pip install -r requirements.txt
1717

18-
# Make port 80 available to the world outside this container
18+
# Make port 8000 available to the world outside this container
1919
EXPOSE 8000
2020

2121
# Define environment variable

Diff for: Linkedin Writeup.docx

13.9 KB
Binary file not shown.

Diff for: Yolo8_Model_Creation.ipynb

+370-57
Large diffs are not rendered by default.

Diff for: coco_names.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
COCO_INSTANCE_CATEGORY_NAMES = [
2+
'__background__', 'person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus',
3+
'train', 'truck', 'boat', 'traffic light', 'fire hydrant', 'N/A', 'stop sign',
4+
'parking meter', 'bench', 'bird', 'cat', 'dog', 'horse', 'sheep', 'cow',
5+
'elephant', 'bear', 'zebra', 'giraffe', 'N/A', 'backpack', 'umbrella', 'N/A', 'N/A',
6+
'handbag', 'tie', 'suitcase', 'frisbee', 'skis', 'snowboard', 'sports ball',
7+
'kite', 'baseball bat', 'baseball glove', 'skateboard', 'surfboard', 'tennis racket',
8+
'bottle', 'N/A', 'wine glass', 'cup', 'fork', 'knife', 'spoon', 'bowl',
9+
'banana', 'apple', 'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza',
10+
'donut', 'cake', 'chair', 'couch', 'potted plant', 'bed', 'N/A', 'dining table',
11+
'N/A', 'N/A', 'toilet', 'N/A', 'tv', 'laptop', 'mouse', 'remote', 'keyboard', 'cell phone',
12+
'microwave', 'oven', 'toaster', 'sink', 'refrigerator', 'N/A', 'book',
13+
'clock', 'vase', 'scissors', 'teddy bear', 'hair drier', 'toothbrush'
14+
]

Diff for: detect.py

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import torchvision
2+
import numpy
3+
import torch
4+
import argparse
5+
import cv2
6+
import detect_utils
7+
from PIL import Image
8+
9+
# construct the argument parser
10+
parser = argparse.ArgumentParser()
11+
parser.add_argument('-i', '--input', help='path to input image/video')
12+
parser.add_argument('-m', '--min-size', dest='min_size', default=800,
13+
help='minimum input size for the FasterRCNN network')
14+
args = vars(parser.parse_args())
15+
16+
# download or load the model from disk
17+
model = torchvision.models.detection.fasterrcnn_resnet50_fpn(pretrained=True,
18+
min_size=args['min_size'])
19+
20+
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
21+
22+
image = Image.open(args['input'])
23+
model.eval().to(device)
24+
boxes, classes, labels = detect_utils.predict(image, model, device, 0.8)
25+
image = detect_utils.draw_boxes(boxes, classes, labels, image)
26+
cv2.imshow('Image', image)
27+
save_name = f"{args['input'].split('/')[-1].split('.')[0]}_{args['min_size']}"
28+
cv2.imwrite(f"outputs/{save_name}.jpg", image)
29+
cv2.waitKey(0)

Diff for: detect_utils.py

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import torchvision.transforms as transforms
2+
import cv2
3+
import numpy as np
4+
from coco_names import COCO_INSTANCE_CATEGORY_NAMES as coco_names
5+
6+
# this will help us create a different color for each class
7+
COLORS = np.random.uniform(0, 255, size=(len(coco_names), 3))
8+
9+
# define the torchvision image transforms
10+
transform = transforms.Compose([
11+
transforms.ToTensor(),
12+
])
13+
14+
def predict(image, model, device, detection_threshold):
15+
# transform the image to tensor
16+
image = transform(image).to(device)
17+
image = image.unsqueeze(0) # add a batch dimension
18+
outputs = model(image) # get the predictions on the image
19+
# print the results individually
20+
# print(f"BOXES: {outputs[0]['boxes']}")
21+
# print(f"LABELS: {outputs[0]['labels']}")
22+
# print(f"SCORES: {outputs[0]['scores']}")
23+
# get all the predicited class names
24+
pred_classes = [coco_names[i] for i in outputs[0]['labels'].cpu().numpy()]
25+
# get score for all the predicted objects
26+
pred_scores = outputs[0]['scores'].detach().cpu().numpy()
27+
# get all the predicted bounding boxes
28+
pred_bboxes = outputs[0]['boxes'].detach().cpu().numpy()
29+
# get boxes above the threshold score
30+
boxes = pred_bboxes[pred_scores >= detection_threshold].astype(np.int32)
31+
return boxes, pred_classes, outputs[0]['labels']
32+
33+
def draw_boxes(boxes, classes, labels, image):
34+
# read the image with OpenCV
35+
image = cv2.cvtColor(np.asarray(image), cv2.COLOR_BGR2RGB)
36+
for i, box in enumerate(boxes):
37+
color = COLORS[labels[i]]
38+
cv2.rectangle(
39+
image,
40+
(int(box[0]), int(box[1])),
41+
(int(box[2]), int(box[3])),
42+
color, 2
43+
)
44+
cv2.putText(image, classes[i], (int(box[0]), int(box[1]-5)),
45+
cv2.FONT_HERSHEY_SIMPLEX, 0.8, color, 2,
46+
lineType=cv2.LINE_AA)
47+
return image

Diff for: main.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@
1515
async def read_index():
1616
return FileResponse('Upload_for_Detection.html')
1717

18-
# Define endpoint for making box predictions
18+
# Define endpoint for making box predictions using Web UI
1919
@app.post("/YOLO_Box_Prediction_Website/")
2020
def predict_uploaded_image(file: UploadFile):
2121

2222
try:
2323

24-
# Upload the image transmitted via POST in a file based on its name
24+
# Upload and open the image transmitted via POST in a file based on its name
2525
file_name = file.filename
2626
with open(file_name, "wb") as f:
2727
f.write(file.file.read())
@@ -41,12 +41,10 @@ def predict_uploaded_image(file: UploadFile):
4141
except Exception as e:
4242
return {"message": e.args}
4343

44-
# Define endpoint for making box predictions
44+
# Define endpoint for making box predictions programatically
4545
@app.post("/YOLO_Box_Prediction_Service/")
4646
async def predict_uploaded_image(file: UploadFile):
4747

48-
#contents = await file.read()
49-
5048
try:
5149

5250
# Upload the image transmitted via POST in a file based on its name

Diff for: results.jpg

267 Bytes
Loading

Diff for: yolov5nu.pt

5.27 MB
Binary file not shown.

0 commit comments

Comments
 (0)