import cv2 import numpy as np # Capture device number cap = cv2.VideoCapture(1) # Set capture resolution cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1280) cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 720) max_count = 1 # Text font = cv2.FONT_HERSHEY_SIMPLEX org = (50, 50) org2 = (50, 90) fontScale = 1 color = (255, 0, 0) thickness = 2 # Subwindow size scale = 6 # Main loop while True: #Grab frame ret, src = cap.read() # Get sub window size center = (int(src.shape[0]/2),int(src.shape[1]/2)) upper_left = (int(center[1] - src.shape[1]/scale),int(center[0] - src.shape[0]/scale)) lower_right = (int(center[1] + src.shape[1]/scale),int(center[0] + src.shape[0]/scale)) # Color conversion gray = cv2.cvtColor(src[upper_left[1]:lower_right[1],upper_left[0]:lower_right[0]], cv2.COLOR_BGR2GRAY) # Edge detection sobel = cv2.Sobel(gray, cv2.CV_8U, 1, 0, ksize=3) # Thresholding operation t, thresh = cv2.threshold(sobel, 200, 255, cv2.THRESH_BINARY) count = np.sum(sobel) if count > max_count: max_count = count # Text rendering src = cv2.putText(src, 'Max: {}'.format(int(max_count/10000)), org, font, fontScale, color, thickness, cv2.LINE_AA) src = cv2.putText(src, 'Current: {:.2f}%'.format((count/max_count)*100 - 0.01), org2, font, fontScale, color, thickness, cv2.LINE_AA) # Subwindow Outline src = cv2.rectangle(src, pt1=upper_left, pt2=lower_right, color=(36,255,12), thickness=1) #Display of results cv2.imshow('tresh', thresh) cv2.imshow('src', src) cv2.imshow('sobel', sobel) # Frame waiting and logic loop k = cv2.waitKey(10) if k == 27: #Escape key to escape break elif k == 32: # Spacebar to reset count max_count = 1