Upload New File

parent 67f2b74b
#############################################
# Object detection - YOLO - OpenCV
# Author : Arun Ponnusamy (July 16, 2018)
# Website : http://www.arunponnusamy.com
############################################
import argparse
import csv
import cv2
import numpy as np
import pandas as pd
#Ref for mov to mp4: http://www.legendu.net/en/blog/python-opencv-python/
VIDEO_STREAM = 'Input_Videos\eyediap\A_1_A_FT_M.mov'
VIDEO_STREAM_OUT = 'Processed_Videos\Datacollection\output_1_A_FT_M2.mov'
fourcc = cv2.VideoWriter_fourcc(*"XVID")
cap = cv2.VideoCapture(VIDEO_STREAM)
ret, frame = cap.read()
# frame = cv2.resize(frame, (640,480), interpolation=cv2.INTER_AREA)
writer = cv2.VideoWriter(VIDEO_STREAM_OUT, fourcc, 30, (frame.shape[1],frame.shape[0]), True)
ap = argparse.ArgumentParser()
ap.add_argument('-i', '--image', required=True,
help = 'path to input image')
ap.add_argument('-c', '--config', required=True,
help = 'path to yolo config file')
ap.add_argument('-w', '--weights', required=True,
help = 'path to yolo pre-trained weights')
ap.add_argument('-cl', '--classes', required=True,
help = 'path to text file containing class names')
args = ap.parse_args()
def get_output_layers(net):
layer_names = net.getLayerNames()
#output_layers = [layer_names[i[0] - 1] for i in net.getUnconnectedOutLayers()]
output_layers = [layer_names[i - 1] for i in net.getUnconnectedOutLayers()]
return output_layers
def draw_prediction(img, class_id, confidence, x, y, x_plus_w, y_plus_h):
print('x - ',x, 'y- ', y, 'x+w = ', x_plus_w, 'y+h = ', y_plus_h)
# print('x1y1 = ',x1_y1, 'x2y2 = ', x2_y2)
# label = str(classes[class_id])
label = 'ball'
# color = COLORS[class_id]
color = 255,0,0
img = cv2.rectangle(img, (x,y), (x_plus_w,y_plus_h), color, 2)
img = cv2.putText(img, label, (x-10,y-10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
return img
# image = cv2.imread(args.image)
Width = frame.shape[1]
Height = frame.shape[0]
scale = 0.00392
print(frame.shape)
classes = None
frame_count = 0
df_center_x,df_center_y, df_width, df_height, df_frame_num, df_x1, df_y1, df_x2, df_y2 = [],[],[],[],[],[],[],[],[]
net = cv2.dnn.readNet(args.weights, args.config)
# COLORS = np.random.uniform(0, 255, size=(len(classes), 3))
while True :
ret, image = cap.read()
# print(ret)
if ret:
# if frame_count == 10:
# break
image = cv2.resize(image, (640,360), interpolation=cv2.INTER_AREA)
frame_count += 1
print(frame_count)
center_x, center_y, x, y,w,h = 0,0,0,0,0,0
with open(args.classes, 'r') as f:
classes = [line.strip() for line in f.readlines()]
blob = cv2.dnn.blobFromImage(image, scale, (416,416), (0,0,0), True, crop=False)
net.setInput(blob)
outs = net.forward(get_output_layers(net))
class_ids = []
confidences = []
boxes = []
conf_threshold = 0.5
nms_threshold = 0.4
for out in outs:
for detection in out:
scores = detection[5:]
class_id = np.argmax(scores)
confidence = scores[class_id]
if confidence > 0.5 and (class_id == 32 or class_id == 29 or class_id == 47 or class_id == 41 ):
# sports ball, frisbee, apple
# and class_id == 24:
center_x = int(detection[0] * Width)
center_y = int(detection[1] * Height)
print(class_id, center_x, center_y)
w = int(detection[2] * Width)
h = int(detection[3] * Height)
x = center_x - w / 2
y = center_y - h / 2
class_ids.append(class_id)
confidences.append(float(confidence))
boxes.append([x, y, w, h])
df_frame_num.append(frame_count)
df_width.append(w)
df_height.append(h)
df_center_x.append(center_x)
df_center_y.append(center_y)
x1 = int(x)
y1 = int(y)
x2 = int(x+w)
y2 = int(y+h)
x1_y1 = (x1, y1)
x2_y2 = (x2, y2)
df_x1.append(x1)
df_y1.append(y1)
df_x2.append(x2)
df_y2.append(y2)
print(x1, ',', y1, '-----',x2, ',', y2 )
print("Center x:", center_x, " center y:", center_y)
indices = cv2.dnn.NMSBoxes(boxes, confidences, conf_threshold, nms_threshold)
for i in indices:
# i = i[0]
box = boxes[i]
x = box[0]
y = box[1]
w = box[2]
h = box[3]
img = draw_prediction(image, class_ids[i], confidences[i], round(x), round(y), round(x+w), round(y+h))
#cv2.imshow("object detection", img)
cv2.waitKey(10)
writer.write(image)
else :
break
df_dict = {'Frame_number': df_frame_num, 'Detector_center(x)': df_center_x, 'Detector_center(y)': df_center_y,'detector_bb_height': df_height, 'detector_bb_width':df_width, 'Detector_x1':df_x1, 'Detector_y1':df_y1, 'Detector_x2':df_x2, 'Detector_y2':df_y2}
df = pd.DataFrame(df_dict)
print(df.head)
df.to_csv("csv/csv_eyediap/detector_output_1_A_FT_MTest.csv", index=False)
# VIDEO_STREAM.release()
writer.release()
cv2.destroyAllWindows()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment