first_frame = gray continue delta_frame = cv2.absdiff(first_frame, gray) thresh_frame = cv2.threshold(delta_frame, 30, 255, cv2.THRESH_BINARY)[1] thresh_frame = cv2.dilate(thresh_frame, None, iterations=2) (cnts, _) = cv2.findContours(thresh_frame.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) for contour in cnts: if cv2.contourArea(contour) < 10000: continue status = 1 (x, y, w, h) = cv2.boundRect(contour) cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 3) status_list.append(status) if status_list[-1] == 1 and status_list[-2] == 0: times.append(datetime.now()) if status_list[-1] == 0 and status_list[-2] == 1: times.append(datetime.now()) cv2.imshow("Gray Frame", gray) cv2.imshow("Delta Frame", delta_frame) cv2.imshow("Threshold Frame", thresh_frame) cv2.imshow("Color Frame", frame) key = cv2.waitKey(1)
# compute the bounding box of the contour (x, y, w, h) = cv2.boundingRect(c) # if the contour is sufficiently large, it must be a digit if w >= 15 and (h >= 30 and h <= 40): digitCnts.append(c) # sort the contours from left-to-right, then initialize the # actual digits themselves digitCnts = contours.sort_contours(digitCnts, method="left-to-right")[0] digits = [] # loop over each of the digits for c in digitCnts: # extract the digit ROI (x, y, w, h) = cv2.boundRect(c) roi = thresh[y:y + h, x:x + w] # compute the width and height of each of the 7 segments # we are gong to examine (roiH, roiW) = roi.shape (dW, dH) = (int(roiW * 0.25), int(roiH * 0.15)) dHC = int(roiH * 0.05) # define the set of 7 segments segments = [ ((0, 0), (w, dH)), # top ((0, 0), (dW, h // 2)), # top-left ((w - dW, 0), (w, h // 2)), # top-right ((0, (h // 2) - dHC), (w, (h // 2) + dHC)), # center ((0, h // 2), (dW, h)), # bottom-left