drsaikirant88
commited on
Commit
•
108582c
1
Parent(s):
acbb319
Delete detect.py
Browse files
detect.py
DELETED
@@ -1,161 +0,0 @@
|
|
1 |
-
# PyTorch implementation of Darknet
|
2 |
-
# This is a custom, hard-coded version of darknet with
|
3 |
-
# YOLOv3 implementation for openimages database. This
|
4 |
-
# was written to test viability of implementing YOLO
|
5 |
-
# for face detection followed by emotion / sentiment
|
6 |
-
# analysis.
|
7 |
-
#
|
8 |
-
# Configuration, weights and data are hardcoded.
|
9 |
-
# Additional options include, ability to create
|
10 |
-
# subset of data with faces exracted for labelling.
|
11 |
-
#
|
12 |
-
# Author : Saikiran Tharimena
|
13 |
-
# Co-Authors: Kjetil Marinius Sjulsen, Juan Carlos Calvet Lopez
|
14 |
-
# Project : Emotion / Sentiment Detection from news images
|
15 |
-
# Date : 12 September 2022
|
16 |
-
# Version : v0.1
|
17 |
-
#
|
18 |
-
# (C) Schibsted ASA
|
19 |
-
|
20 |
-
# Libraries
|
21 |
-
import os
|
22 |
-
import cv2
|
23 |
-
import torch
|
24 |
-
import numpy as np
|
25 |
-
from utils import *
|
26 |
-
from darknet import Darknet
|
27 |
-
from torch.autograd import Variable
|
28 |
-
from torch.cuda import is_available as check_cuda
|
29 |
-
|
30 |
-
# Parameters
|
31 |
-
batch_size = 1
|
32 |
-
confidence = 0.25
|
33 |
-
nms_thresh = 0.30
|
34 |
-
run_cuda = False
|
35 |
-
|
36 |
-
# CFG Files
|
37 |
-
cwd = os.path.dirname(__file__)
|
38 |
-
cfg = cwd + '/cfg/yolov3-openimages.cfg'
|
39 |
-
data = cwd + '/cfg/openimages.data'
|
40 |
-
clsnames= cwd + '/cfg/openimages.names'
|
41 |
-
weights = cwd + '/cfg/yolov3-openimages.weights'
|
42 |
-
|
43 |
-
# Load classes
|
44 |
-
num_classes = 601
|
45 |
-
classes = load_classes(clsnames)
|
46 |
-
|
47 |
-
# Set up the neural network
|
48 |
-
print('Load Network')
|
49 |
-
model = Darknet(cfg)
|
50 |
-
|
51 |
-
print('Load Weights')
|
52 |
-
model.load_weights(weights)
|
53 |
-
|
54 |
-
print('Successfully loaded Network')
|
55 |
-
|
56 |
-
# Check CUDA
|
57 |
-
if run_cuda:
|
58 |
-
CUDA = check_cuda()
|
59 |
-
else:
|
60 |
-
CUDA = False
|
61 |
-
|
62 |
-
# Input dimension
|
63 |
-
inp_dim = int(model.net_info["height"])
|
64 |
-
|
65 |
-
# put the model on GPU
|
66 |
-
if CUDA:
|
67 |
-
model.cuda()
|
68 |
-
|
69 |
-
# Set the model in evaluation mode
|
70 |
-
model.eval()
|
71 |
-
|
72 |
-
# face detector
|
73 |
-
def detect_face(image):
|
74 |
-
# Just lazy to update this
|
75 |
-
imlist = [image]
|
76 |
-
|
77 |
-
loaded_ims = [cv2.imread(x) for x in imlist]
|
78 |
-
|
79 |
-
im_batches = list(map(prep_image, loaded_ims, [inp_dim for x in range(len(imlist))]))
|
80 |
-
im_dim_list = [(x.shape[1], x.shape[0]) for x in loaded_ims]
|
81 |
-
im_dim_list = torch.FloatTensor(im_dim_list).repeat(1,2)
|
82 |
-
|
83 |
-
leftover = 0
|
84 |
-
if (len(im_dim_list) % batch_size):
|
85 |
-
leftover = 1
|
86 |
-
|
87 |
-
if batch_size != 1:
|
88 |
-
num_batches = len(imlist) // batch_size + leftover
|
89 |
-
im_batches = [torch.cat((im_batches[i*batch_size : min((i + 1)*batch_size,
|
90 |
-
len(im_batches))])) for i in range(num_batches)]
|
91 |
-
|
92 |
-
write = 0
|
93 |
-
if CUDA:
|
94 |
-
im_dim_list = im_dim_list.cuda()
|
95 |
-
|
96 |
-
for i, batch in enumerate(im_batches):
|
97 |
-
# load the image
|
98 |
-
|
99 |
-
if CUDA:
|
100 |
-
batch = batch.cuda()
|
101 |
-
with torch.no_grad():
|
102 |
-
prediction = model(Variable(batch), CUDA)
|
103 |
-
|
104 |
-
prediction = write_results(prediction, confidence, num_classes, nms_conf = nms_thresh)
|
105 |
-
|
106 |
-
if type(prediction) == int:
|
107 |
-
|
108 |
-
for im_num, image in enumerate(imlist[i*batch_size: min((i + 1)*batch_size, len(imlist))]):
|
109 |
-
im_id = i*batch_size + im_num
|
110 |
-
|
111 |
-
continue
|
112 |
-
|
113 |
-
prediction[:,0] += i*batch_size # transform the atribute from index in batch to index in imlist
|
114 |
-
|
115 |
-
if not write: # If we have't initialised output
|
116 |
-
output = prediction
|
117 |
-
write = 1
|
118 |
-
else:
|
119 |
-
output = torch.cat((output, prediction))
|
120 |
-
|
121 |
-
for im_num, image in enumerate(imlist[i*batch_size: min((i + 1)*batch_size, len(imlist))]):
|
122 |
-
im_id = i * batch_size + im_num
|
123 |
-
objs = [classes[int(x[-1])] for x in output if int(x[0]) == im_id]
|
124 |
-
|
125 |
-
if CUDA:
|
126 |
-
torch.cuda.synchronize()
|
127 |
-
|
128 |
-
try:
|
129 |
-
output
|
130 |
-
except NameError:
|
131 |
-
return None
|
132 |
-
|
133 |
-
im_dim_list = torch.index_select(im_dim_list, 0, output[:,0].long())
|
134 |
-
|
135 |
-
scaling_factor = torch.min(608/im_dim_list,1)[0].view(-1,1)
|
136 |
-
|
137 |
-
output[:, [1,3]] -= (inp_dim - scaling_factor*im_dim_list[:,0].view(-1,1))/2
|
138 |
-
output[:, [2,4]] -= (inp_dim - scaling_factor*im_dim_list[:,1].view(-1,1))/2
|
139 |
-
|
140 |
-
output[:, 1:5] /= scaling_factor
|
141 |
-
|
142 |
-
for i in range(output.shape[0]):
|
143 |
-
output[i, [1,3]] = torch.clamp(output[i, [1,3]], 0.0, im_dim_list[i,0])
|
144 |
-
output[i, [2,4]] = torch.clamp(output[i, [2,4]], 0.0, im_dim_list[i,1])
|
145 |
-
|
146 |
-
def get_detections(x, results):
|
147 |
-
c1 = [int(y) for y in x[1:3]]
|
148 |
-
c2 = [int(y) for y in x[3:5]]
|
149 |
-
|
150 |
-
det_class = int(x[-1])
|
151 |
-
label = "{0}".format(classes[det_class])
|
152 |
-
|
153 |
-
return (label, tuple(c1 + c2))
|
154 |
-
|
155 |
-
detections = list(map(lambda x: get_detections(x, loaded_ims), output))
|
156 |
-
|
157 |
-
if CUDA:
|
158 |
-
torch.cuda.empty_cache()
|
159 |
-
|
160 |
-
return loaded_ims[0], detections
|
161 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|