import numpy as np import cv2 as cv from google.colab.patches import cv2_imshow import glob import os # Access Files from google.colab import drive drive.mount("gdrive") # Video to Images import cv2 as cv from google.colab.patches import cv2_imshow video = "" # Path to video file capture = cv.VideoCapture(video) isTrue, frame = capture.read() count = 0 while isTrue: if not isTrue: break isTrue, frame = capture.read() cv.imwrite("frame%03d.jpg" %count, frame) # String format '%' is placeholder count += 1 # Class Names importClassNames = "coco.names" classNames = [] with open(importClassNames, "r") as f: classNames = f.read().split("\n") # DNN modelConfiguation = "yolov3.cfg" modelWeights = "yolov3.weights" net = cv.dnn.readNet(modelConfiguation, modelWeights) # Get output layer def getOutputs(frame): blob = cv.dnn.blobFromImage(frame, 1/255, (widthheight, widthheight), [0, 0, 0], 1, crop = False) net.setInput(blob) layerNames = net.getLayerNames() #gets all names of the layer outputNames = [layerNames[i[0] - 1] for i in net.getUnconnectedOutLayers()] outputs = net.forward(outputNames) # output is a list containing 3 values, within the list 'numpy' return outputs # Display Box def displayBox(outputs): height, width, channels = frame.shape boundingBox = [] classIDs = [] confidenceValues = [] for output in outputs: for values in output: scores = values[5:] classID = np.argmax(scores) confidence = scores[classID] if confidence > confidenceThreshold: w, h = int(values[2] * width), int(values[3] * height) x, y = int(values[0] * width - w/2), int(values[1] * height - h/2) boundingBox.append([x, y, w, h]) classIDs.append(classID) confidenceValues.append(float(confidence)) outputBox = cv.dnn.NMSBoxes(boundingBox, confidenceValues, confidenceThreshold, nmsThreshold) for i in outputBox: index = i[0] color = colors[classIDs[index]] x, y, w, h = boundingBox[index][:4] cv.rectangle(frame, (x, y), (x + w, y + h), color, 2) label = str(classNames[classIDs[index]].capitalize()) + " " + str(int(confidenceValues[index] * 100)) + " %" cv.putText(frame, label,(x + 5,y + 16), font, 0.5, color, 2) #Main Function widthheight = 320 confidenceThreshold = 0.5 nmsThreshold = 0.6 font = cv.FONT_HERSHEY_SIMPLEX colors = np.random.uniform(0, 255, size = (len(classNames),3)) path = "/content/" pathFile = path + "*.*" files = sorted(glob.glob(pathFile), key = os.path.getmtime) frameSize = 0.3 for f in files: print(f.strip(path)) frame = cv.imread(f) frame = cv.resize(frame, None, fx = frameSize, fy = frameSize) displayBox(getOutputs(frame)) cv2_imshow(frame)