"No one is harder on a talented person than the person themselves" - Linda Wilkinson ; "Trust your guts and don't follow the herd" ; "Validate direction not destination" ;

June 07, 2021

OpenCV Experiments

Experiment with

  • Logging
  • Threads
  • Error Handling
  • Writing text on Frame with grey background


#Logging
#Frame processing
#Video Stats Frames per second
#Custom text with background
import cv2
import numpy as np
import logging
import time
#Initialize Logging module
#now we will Create and configure logger
logging.basicConfig(filename=r"C:\demo\videolog.log",
format='%(asctime)s %(message)s',
filemode='a')
#Let us Create an object
logger=logging.getLogger()
#Now we are going to Set the threshold of logger to DEBUG
logger.setLevel(logging.DEBUG)
# Create a VideoCapture object and read from input file
cap = cv2.VideoCapture(r'C:\demo\video2.mp4')
# Check if camera opened successfully
if (cap.isOpened()== False):
print("Error opening video file")
icount = 0
#### overlay space
x,y,w,h = 10,30,700,30
#Check frames per second
fps = cap.get(cv2.CAP_PROP_FPS)
# Read until video is completed
while(cap.isOpened()):
# Capture frame-by-frame
ret, frame_source = cap.read()
if ret == True:
frame = cv2.resize(frame_source, (800,600))
# Draw black background rectangle
cv2.rectangle(frame, (x, x), (x + w, y + h), (0,0,0), -1)
frmtext = 'Frames Per Second-' + str(fps) + ' - Frame Count - ' + str(icount)
# Add text
cv2.putText(frame, frmtext, (x + int(w/10),y + int(h/2)), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255,255,255), 2)
logger.info(frmtext)
# Display the resulting frame
cv2.imshow('Frame', frame)
# Press Q on keyboard to exit
if cv2.waitKey(25) & 0xFF == ord('q'):
break
icount = icount+1
# Break the loop
else:
break
# When everything done, release
# the video capture object
cap.release()
# Closes all the frames
cv2.destroyAllWindows()
logging.shutdown()
view raw VideoExample.py hosted with ❤ by GitHub
#Thread with parameters
from threading import Thread
import time
def read_value(i):
print("Read value is: " + str(i))
i = 0
while i < 10:
read_thread = Thread(target=read_value,args=(i,))
read_thread.start()
i = i + 1


import cv2
import datetime
vcap = cv2.VideoCapture(r'C:\Users\video3.mp4') # 0=camera
if vcap.isOpened():
width = vcap.get(cv2.CAP_PROP_FRAME_WIDTH)
height = vcap.get(cv2.CAP_PROP_FRAME_HEIGHT)
fps = vcap.get(cv2.CAP_PROP_FPS)
frames = vcap.get(cv2.CAP_PROP_FRAME_COUNT)
seconds = int(frames / fps)
video_time = str(datetime.timedelta(seconds=seconds))
print("duration in seconds:", seconds)
print("video time:", video_time)
print("width:", width)
print("Height:", height)
view raw videostats.py hosted with ❤ by GitHub



#Mulththread and call back
#Thread with parameters
#https://pythonexamples.org/python-callback-function/
from threading import Thread
import time
def callbackFunc1(results):
print('results - callback')
print(results[0])
print(results[1])
def read_value(i,callbackFunc1):
print("Read value is: " + str(i))
square = i*i
cube = i*i*i
results = [square,cube]
callbackFunc1(results)
i = 0
while i < 10:
read_thread = Thread(target=read_value,args=(i,callbackFunc1,))
read_thread.start()
i = i + 1

Keep Exploring!!!

No comments: