forked from BurhanUlTayyab/GSOC2018_RedHen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
work.py
69 lines (56 loc) · 1.94 KB
/
work.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
'''
Prototype Scrolling Ticker Algorithm in Python
For Google Summer of Code 2018 (Red Hen Labs)
Burhan Ul Tayyab
'''
import cv2
import numpy as np
import sys
cap = cv2.VideoCapture(sys.argv[1])
thresh = 70
kernel = np.ones((8,5),np.uint8)
name = "img"
k = 0
t = 0
while(cap.isOpened()):
#Reading Frames
ret, frame = cap.read()
#Creating a Rectangle (Bounding Box) over Scrolling Tickers
cv2.rectangle(frame, (100, 525), (715, 550), (255,0,0), 2)
#cv2.imshow('frame',frame)
#Cropping the image
crop_img = frame[525:525+25, 100:100+615]
cv2.imshow('framex',crop_img)
#Detecting Blue Areas for Ticker Stopping Point
hsv = cv2.cvtColor(crop_img, cv2.COLOR_BGR2HSV)
lower_red = np.array([110,50,50])
upper_red = np.array([130,255,255])
mask = cv2.inRange(hsv, lower_red, upper_red)
res = cv2.bitwise_and(crop_img,crop_img, mask= mask)
#Graying and Thresholding for finding Contours
gray=cv2.cvtColor(res,cv2.COLOR_BGR2GRAY)
ret,thresh1 = cv2.threshold(gray,thresh,255,cv2.THRESH_BINARY)
closing = cv2.morphologyEx(thresh1, cv2.MORPH_CLOSE, kernel)
t = t + 1
#t,contours, hier = cv2.findContours(thresh1,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)
m, contours, _ = cv2.findContours(closing, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
for cnt in contours:
k = k + 1
rect = cv2.boundingRect(cnt)
if rect[0] > 300 and rect[0] < 450:
print(rect[0])
print(cnt[0])
cv2.drawContours(crop_img,[cnt],0,(0,255,0),2)
record = 1
cv2.imshow('framxds', crop_img)
elif rect[0] < 150:
#vis = np.concatenate((crop_img, crop_img), axis=1)
save = name + str(k) + str(t) + ".jpg"
cv2.imwrite(save, crop_img)
cv2.imshow('framxdss', vis)
cv2.imshow('res',closing)
cv2.imshow('fx',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()