Commit a0abe205 authored by Gihan Jayatilaka's avatar Gihan Jayatilaka

screen detector 3

parent 55e45e29
#python screen-detection-3.py ./inputFile.mp4 ./output.mp4
#python screen-detection-3.py ./inputFile.npz./output.npz
DEBUG=True
RESIZE_RATIO=0.3
import sys
import cv2
import numpy as np
import sklearn
import numpy as np
clicksXY=[]
finishGettingInput=False
vidIn = None
vidOut = None
frame = None
matIdx=0
X=None
Y=None
def resizedFrame(frame,ratio=RESIZE_RATIO):
return cv2.resize(frame,(int(frame.shape[1]*ratio),int(frame.shape[0]*ratio)))
def mouseCallbackVideo(event, x, y, flags, params):
global frame,vidIn,vidOut,clicksXY,finishGettingInput
if event==1:
clicksXY.append([x,y])
while len(clicksXY)>4:
clicksXY.pop(0)
print(x,y)
ret,frame=readFrame(vidIn)
assert ret, "Error in reading video"
for i in range(len(clicksXY)):
cv2.circle(frame,(clicksXY[i][0],clicksXY[i][1]),4,[255,255,255],thickness=1)
cv2.line(frame,(clicksXY[i][0],clicksXY[i][1]),(clicksXY[(i+1)%len(clicksXY)][0],clicksXY[(i+1)%len(clicksXY)][1]),[255,255,255],thickness=1)
cv2.imshow("image",frame)
if event==cv2.EVENT_FLAG_RBUTTON:
finishGettingInput=True
def mouseCallBackNpz(event,x,y,flags,params):
global frame,vidIn,vidOut,clicksXY,finishGettingInput,matIdx,X,Y
if event==1:
clicksXY.append([x,y])
while len(clicksXY)>4:
clicksXY.pop(0)
print(x,y)
matIdx+=1
for i in range(len(clicksXY)):
cv2.circle(X[matIdx],(clicksXY[i][0],clicksXY[i][1]),4,[255,255,255],thickness=1)
cv2.line(X[matIdx],(clicksXY[i][0],clicksXY[i][1]),(clicksXY[(i+1)%len(clicksXY)][0],clicksXY[(i+1)%len(clicksXY)][1]),[255,255,255],thickness=1)
cv2.imshow("frame",X[matIdx])
if event==cv2.EVENT_FLAG_RBUTTON:
finishGettingInput=True
def readFrame(vid):
ret,frame=vid.read()
if not ret:
return False, None
else:
frame=resizedFrame(frame)
return True, frame
def mainFuncVideo():
global frame,vidIn,vidOut,clicksXY,finishGettingInput,X,Y
inputFile=sys.argv[1]#"./video/real00.avi"
outputFile="./tempOut.mp4"#sys.argv[2]
if len(sys.argv)>2:
outputFile=sys.argv[2]
vidIn=cv2.VideoCapture(inputFile)
vidOut=cv2.VideoWriter(outputFile,cv2.VideoWriter_fourcc('M','J','P','G'),60,(299,299))
ret,frame=readFrame(vidIn)
assert ret , "No frame returned"
cv2.imshow("image",frame)
cv2.setMouseCallback('image', mouseCallbackVideo)
while not finishGettingInput:
cv2.waitKey(1)
cv2.destroyWindow("image")
vidIn.release()
vidIn=cv2.VideoCapture(inputFile)
destPoints="[[0,0], [298,0], [298,298], [0,298]]"#sys.argv[3]##
sourcePoints="[[{},{}], [{},{}], [{},{}], [{},{}]]".format(clicksXY[0][0],clicksXY[0][1],clicksXY[1][0],clicksXY[1][1],clicksXY[2][0],clicksXY[2][1],clicksXY[3][0],clicksXY[3][1])#sys.argv[4]
sourcePoints=np.array(eval(sourcePoints)).astype(np.float32)
destPoints=np.array(eval(destPoints)).astype(np.float32)
print("Source points shape = {}, dest = {}".format(sourcePoints.shape,destPoints.shape))
h=cv2.getPerspectiveTransform(sourcePoints,destPoints)
while True:
ret,frame=readFrame(vidIn)
if not ret:
break
destFrame = cv2.warpPerspective(frame, h, (299,299))
cv2.imshow("Oriented frame",destFrame)
vidOut.write(destFrame)
for i in range(len(clicksXY)):
cv2.circle(frame,(clicksXY[i][0],clicksXY[i][1]),4,[255,255,255],thickness=1)
cv2.line(frame,(clicksXY[i][0],clicksXY[i][1]),(clicksXY[(i+1)%len(clicksXY)][0],clicksXY[(i+1)%len(clicksXY)][1]),[255,255,255],thickness=1)
cv2.imshow("Original frame",frame)
cv2.waitKey(1)
cv2.destroyAllWindows()
vidIn.release()
vidOut.release()
def mainFuncNpz():
global frame,vidIn,vidOut,clicksXY,matIdx,finishGettingInput,X,Y
inputFile=sys.argv[1]#"./video/real00.avi"
outputFile="./tempOut.npz"#sys.argv[2]
if len(sys.argv)>2:
outputFile=sys.argv[2]
data=np.load(inputFile)
X=data['X']
Y=data['Y']
data=None
cv2.imshow("frame",X[matIdx])
cv2.setMouseCallback('frame', mouseCallBackNpz)
while not finishGettingInput:
cv2.waitKey(10)
matIdx=0
XX=np.zeros(shape = (X.shape[0],299,299,3), dtype = np.uint8)
destPoints="[[0,0], [298,0], [298,298], [0,298]]"#sys.argv[3]##
sourcePoints="[[{},{}], [{},{}], [{},{}], [{},{}]]".format(clicksXY[0][0],clicksXY[0][1],clicksXY[1][0],clicksXY[1][1],clicksXY[2][0],clicksXY[2][1],clicksXY[3][0],clicksXY[3][1])#sys.argv[4]
sourcePoints=np.array(eval(sourcePoints)).astype(np.float32)
destPoints=np.array(eval(destPoints)).astype(np.float32)
print("Source points shape = {}, dest = {}".format(sourcePoints.shape,destPoints.shape))
h=cv2.getPerspectiveTransform(sourcePoints,destPoints)
while True:
if not matIdx < X.shape[0]:
break
XX[matIdx]= cv2.warpPerspective(X[matIdx], h, (299,299))
cv2.imshow("Oriented frame",XX[matIdx])
for i in range(len(clicksXY)):
cv2.circle(X[matIdx],(clicksXY[i][0],clicksXY[i][1]),4,[255,255,255],thickness=1)
cv2.line(X[matIdx],(clicksXY[i][0],clicksXY[i][1]),(clicksXY[(i+1)%len(clicksXY)][0],clicksXY[(i+1)%len(clicksXY)][1]),[255,255,255],thickness=1)
cv2.imshow("Original frame",X[matIdx])
cv2.waitKey(1)
matIdx+=1
np.save(outputFile,X=XX,Y=Y)
cv2.destroyAllWindows()
if __name__ == '__main__':
if sys.argv[1].strip().split(".")[-1]=="npz":
mainFuncNpz()
else: mainFuncVideo()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment