Learn practical skills, build real-world projects, and advance your career
Created 3 years ago
Comapct Version - Batch-wise processing
# Import modules
import numpy as np
import imutils
import sys
import cv2
# load the contents of the class labels file, then define the sample
# duration (i.e., # of frames for classification) and sample size
# (i.e., the spatial dimensions of the frame)
CLASSES = open('action_recognition_kinetics.txt').read().strip().split("\n")
SAMPLE_DURATION = 16
SAMPLE_SIZE = 112
# load the human activity recognition model
print("[INFO] loading human activity recognition model...")
net = cv2.dnn.readNet('resnet-34_kinetics.onnx')
# grab a pointer to the input video stream
print("[INFO] accessing video stream...")
cap = cv2.VideoCapture('bike-2.mp4')
# loop until we explicitly break from it
while True:
frames = []
# Loop for every 'SAMPLE_DURATION' frames
for i in range(0, SAMPLE_DURATION):
# read each frame
grabbed, frame = cap.read()
# If no frame exists or end of the video.
if not grabbed:
print("[Info] No frame read from stream - exiting")
sys.exit(0)
# Not end of the video
frame = imutils.resize(frame, width=400)
frames.append(frame)
# now that our frames array is filled we can construct our blob
blob = cv2.dnn.blobFromImages(frames, 1.0, (SAMPLE_SIZE, SAMPLE_SIZE), (114.7748, 107.7354, 99.4750),
swapRB=True, crop=True)
blob = np.transpose(blob, (1, 0, 2, 3))
blob = np.expand_dims(blob, axis=0)
# pass the blob through the model to obtain our human activity recognition predictions!!
net.setInput(blob)
outputs = net.forward()
label = CLASSES[np.argmax(outputs)]
# loop over our frames
for frame in frames:
# draw the predicted activity on the frame
cv2.rectangle(frame, (0, 0), (300, 40), (0, 0, 0), -1)
cv2.putText(frame, label, (10, 25), cv2.FONT_HERSHEY_SIMPLEX,
0.8, (255, 255, 255), 2)
# display the frame to our screen
cv2.imshow("Activity Recognition", frame)
if cv2.waitKey(1)==ord('q'):
break
cap.release()
cv2.destroyAllWindows()
[INFO] loading human activity recognition model...
[INFO] accessing video stream...
[Info] No frame read from stream - exiting
An exception has occurred, use %tb to see the full traceback.
SystemExit: 0
c:\users\vsneh\appdata\local\programs\python\python37\lib\site-packages\IPython\core\interactiveshell.py:3334: UserWarning: To exit: use 'exit', 'quit', or Ctrl-D.
warn("To exit: use 'exit', 'quit', or Ctrl-D.", stacklevel=1)
Effective Version (DeQueue method) - Frame by Frame processing
# Import necessary packages
from collections import deque
import numpy as np
import argparse
import imutils
import cv2
import matplotlib.pyplot as plt
- Warning images to Overlay
- Helper Function