def find_unchange_pos(video_path): video = cv2.VideoCapture(video_path) width = int(video.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH)) height = int(video.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT)) frames = video.get(cv2.cv.CV_CAP_PROP_FRAME_COUNT) fps = video.get(cv2.cv.CV_CAP_PROP_FPS) red = [0x0, 0x0, 0xFF] step = int(frames / 500) # read 500 frames in total print width, height, step pos = 0 last = '' while (video.isOpened): if pos % step == 0: ret, cur = video.read() bin_cur = op.get_binary(cur) else: ret = video.grab() if not ret: break if pos % step != 0: pos += 1 continue cv2.imshow('cur', cur) if pos != 0: # first frame print 'begin calc' # diff = mc.diff(cur,last) for h in range(height): for w in range(width): if bin_cur[h][w] == last[h][w]: cur[h][w] = red continue ch, cw = h, w # copy while ch < height and bin_cur[ch][w] == bin_cur[h][ w]: #almost same color ch += 1 if ch - h < 10: continue while cw < width and bin_cur[h][cw] == bin_cur[h][w]: cw += 1 if cw - w < 30: continue cv2.rectangle(cur, (cw, h), (w, ch), red) cv2.imshow('last', last) cv2.imshow('cur', cur) key = cv2.waitKey(0) if key == ord('q'): break last = bin_cur pos += 1 print pos cv2.destroyAllWindows() video.release()
def find_unchange_pos(video_path): video = cv2.VideoCapture(video_path) width = int(video.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH)) height = int(video.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT)) frames = video.get(cv2.cv.CV_CAP_PROP_FRAME_COUNT) fps = video.get(cv2.cv.CV_CAP_PROP_FPS) red = [0x0,0x0,0xFF] step = int(frames / 500) # read 500 frames in total print width, height, step pos = 0 last = '' while(video.isOpened): if pos % step == 0: ret, cur = video.read() bin_cur = op.get_binary(cur) else: ret = video.grab() if not ret: break if pos % step != 0: pos += 1 continue cv2.imshow('cur', cur) if pos != 0: # first frame print 'begin calc' # diff = mc.diff(cur,last) for h in range(height): for w in range(width): if bin_cur[h][w]==last[h][w]: cur[h][w] = red continue ch, cw = h,w # copy while ch < height and bin_cur[ch][w]==bin_cur[h][w]: #almost same color ch += 1 if ch - h < 10: continue while cw < width and bin_cur[h][cw]==bin_cur[h][w]: cw += 1 if cw - w < 30: continue cv2.rectangle(cur, (cw,h),(w,ch), red) cv2.imshow('last', last) cv2.imshow('cur', cur) key = cv2.waitKey(0) if key == ord('q'): break last = bin_cur pos += 1 print pos cv2.destroyAllWindows() video.release()
def find_lines(video_path): video = cv2.VideoCapture(video_path) width = int(video.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH)) height = int(video.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT)) frames = video.get(cv2.cv.CV_CAP_PROP_FRAME_COUNT) fps = video.get(cv2.cv.CV_CAP_PROP_FPS) red = [0x0,0x0,0xFF] step = int(fps * 3) # step 3 seconds total_frames = 500 print width, width, step pos = 0 last = '' # skip first ten minutes video.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, fps * 60 * 10) line_appear = np.zeros(width,dtype=np.uint8) i = 0 while video.isOpened and i < total_frames: if pos % step == 0: ret, cur = video.read() i += 1 print i else: ret = video.grab() if not ret: break if pos % step != 0: pos += 1 continue # binary = op.get_binary(cur) # contours, hierarchy = cv2.findContours(binary, cv2.RETR_EXTERNAL ,cv2.CHAIN_APPROX_SIMPLE) # cv2.drawContours(cur, contours,-1,(0,0,255),1) binary = op.get_binary(cur) edges = cv2.Canny(binary,50,150,apertureSize = 3) lines = cv2.HoughLines(edges,1,np.pi/180,200) # print edges, lines if lines != None: for rho, theta in lines[0]: # only horizontal line reserved if np.sin(theta) != 1: continue # intersect is rho # print rho line_appear[int(rho)] += 1 cv2.line(cur,(0,rho),(width,rho),red,2) cv2.imshow('cur', cur) pos += 1 key = cv2.waitKey(1) if key == ord('q'): break # print pos max_cnt = max(line_appear) valid_lines = [] last_h = -1 for h in range(width): if line_appear[h] * 3 > max_cnt: if last_h == -1: last_h = h elif h - last_h > 10 and h - last_h < 50: valid_lines.append((last_h,h)) last_h = h return valid_lines
def get_text(board): board = op.get_binary(board) cv2.imshow('bin', board) height, width = board.shape # print height,width left,right = 0,0 imgs = [] pos = [] row = 0 # eliminate left few columns that are not backgound while row < width and sum(board[0:,row]) != 0 and sum(board[0:,row])!=255 * height: row += 1 while row < width: #sum of this row background = 0 pixel_sum = sum(board[0:,row]) while pixel_sum == 0 or pixel_sum == 255 * height: # eliminate background background = pixel_sum row += 1 if row >= width: break pixel_sum = sum(board[0:,row]) # print 'x',pixel_sum, row left = row if row >= width: break pixel_sum = sum(board[0:,row]) while row < width and pixel_sum != 0 and pixel_sum != 255 * height: row += 1 if row >= width: break pixel_sum = sum(board[0:,row]) right = row # print left,right # if this interval satisfy our need if right - left < 3 or right - left > 40: continue single = board[0:,left:right+1] # convert background to black if background == 0: single = ~single # eliminate top and bottom backgoound top = 0 while top < height and sum(single[top,0:])==255*(right-left+1): top +=1 bottom = height - 1 while bottom > top and sum(single[bottom,0:])==255*(right-left+1): bottom -= 1 if bottom - top < 10 or bottom - top < height / 2: continue pos.append((left,top)) img = cv2.resize(single[top:bottom+1],(24,24)) imgs.append(img) return imgs, pos
def get_text(board): board = op.get_binary(board) cv2.imshow('bin', board) height, width = board.shape # print height,width left, right = 0, 0 imgs = [] pos = [] row = 0 # eliminate left few columns that are not backgound while row < width and sum(board[0:, row]) != 0 and sum( board[0:, row]) != 255 * height: row += 1 while row < width: #sum of this row background = 0 pixel_sum = sum(board[0:, row]) while pixel_sum == 0 or pixel_sum == 255 * height: # eliminate background background = pixel_sum row += 1 if row >= width: break pixel_sum = sum(board[0:, row]) # print 'x',pixel_sum, row left = row if row >= width: break pixel_sum = sum(board[0:, row]) while row < width and pixel_sum != 0 and pixel_sum != 255 * height: row += 1 if row >= width: break pixel_sum = sum(board[0:, row]) right = row # print left,right # if this interval satisfy our need if right - left < 3 or right - left > 40: continue single = board[0:, left:right + 1] # convert background to black if background == 0: single = ~single # eliminate top and bottom backgoound top = 0 while top < height and sum(single[top, 0:]) == 255 * (right - left + 1): top += 1 bottom = height - 1 while bottom > top and sum(single[bottom, 0:]) == 255 * (right - left + 1): bottom -= 1 if bottom - top < 10 or bottom - top < height / 2: continue pos.append((left, top)) img = cv2.resize(single[top:bottom + 1], (24, 24)) imgs.append(img) return imgs, pos
def find_lines(video_path): video = cv2.VideoCapture(video_path) width = int(video.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH)) height = int(video.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT)) frames = video.get(cv2.cv.CV_CAP_PROP_FRAME_COUNT) fps = video.get(cv2.cv.CV_CAP_PROP_FPS) red = [0x0, 0x0, 0xFF] step = int(fps * 3) # step 3 seconds total_frames = 500 print width, width, step pos = 0 last = '' # skip first ten minutes video.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, fps * 60 * 10) line_appear = np.zeros(width, dtype=np.uint8) i = 0 while video.isOpened and i < total_frames: if pos % step == 0: ret, cur = video.read() i += 1 print i else: ret = video.grab() if not ret: break if pos % step != 0: pos += 1 continue # binary = op.get_binary(cur) # contours, hierarchy = cv2.findContours(binary, cv2.RETR_EXTERNAL ,cv2.CHAIN_APPROX_SIMPLE) # cv2.drawContours(cur, contours,-1,(0,0,255),1) binary = op.get_binary(cur) edges = cv2.Canny(binary, 50, 150, apertureSize=3) lines = cv2.HoughLines(edges, 1, np.pi / 180, 200) # print edges, lines if lines != None: for rho, theta in lines[0]: # only horizontal line reserved if np.sin(theta) != 1: continue # intersect is rho # print rho line_appear[int(rho)] += 1 cv2.line(cur, (0, rho), (width, rho), red, 2) cv2.imshow('cur', cur) pos += 1 key = cv2.waitKey(1) if key == ord('q'): break # print pos max_cnt = max(line_appear) valid_lines = [] last_h = -1 for h in range(width): if line_appear[h] * 3 > max_cnt: if last_h == -1: last_h = h elif h - last_h > 10 and h - last_h < 50: valid_lines.append((last_h, h)) last_h = h return valid_lines