Код:
import time
import numpy as np
import matplotlib.pyplot as plt
vid = cv2.VideoCapture('4.mp4')
while True:
#cv2.namedWindow('frame',cv2.WINDOW_NORMAL)
ret, img_color = vid.read()
if not ret:
vid = cv2.VideoCapture('5.mp4')
continue
num_rows, num_cols = img_color.shape[:2]
rotation_matrix = cv2.getRotationMatrix2D((num_cols/2, num_rows/2), 270, 0.56) #3
img_rotated = cv2.warpAffine(img_color, rotation_matrix, (num_cols, num_rows))
height, width = img_rotated.shape[:2]
img_resize = cv2.resize(img_rotated,(int(0.8*width), int(0.8*height)), interpolation = cv2.INTER_CUBIC) #2
img_clone = img_resize[10:842,530:1000].copy()
img_roi = img_resize[10+250:842-200,530:1000]
img_gray = cv2.cvtColor(img_roi,cv2.COLOR_BGR2GRAY) #1
kernel = [ [0,-1,0], [-1,5,-1], [0,-1,0] ]
kernel = np.array(kernel)
img_sharp = cv2.filter2D(img_gray,-1,kernel)
blur = cv2.GaussianBlur(img_sharp,(5,5),0)
img_canny = cv2.Canny(blur,130,170, apertureSize = 3) #4
lines = cv2.HoughLinesP(img_canny, 1, np.pi/180, 60, maxLineGap = 240)
if lines is not None:
print(len(lines))
for line in lines:
x1,y1,x2,y2 = line[0]
cv2.line(img_clone, (x1,y1+250), (x2,y2+250), (0,255,0), 2)
#cv2.line(img_clone, (x1,y1), (x2,y2), (255,255,0), 3)
cv2.imshow('frame',img_clone)
cv2.imshow('frame2', img_canny)
k = cv2.waitKey(35) & 0xFF
if k==27 :
break
vid.release()
cv2.destroyAllWindows()