Commit 67402f8c authored by Gihan Jayatilaka's avatar Gihan Jayatilaka

sc-detect-5

parent 85a1ad42
DEBUG=True
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
import cv2
import sys
from keras.layers import *
from keras import Model, Sequential
from keras.activations import relu,sigmoid
def getModel(xShape,yShape):
print("xShape={} yShape={}".format(xShape,yShape))
mod=Sequential()
mod.add(Conv2D(10,kernel_size=(2,2),strides=(2,2),input_shape=xShape,input_dtype=np.float32,activation='relu'))
mod.add(Conv2D(20,kernel_size=(2,2),strides=(2,2),activation='relu'))
mod.add(Conv2D(40,kernel_size=(2,2),strides=(2,2),activation='relu'))
mod.add(Conv2D(80,kernel_size=(2,2),strides=(2,2),activation='relu'))
mod.add(Conv2D(100,kernel_size=(2,2),strides=(2,2),activation='relu'))
mod.add(Flatten())
mod.add(Dense(40,activation='relu'))
mod.add(Dense(8,activation='relu'))
mod.compile(optimizer='adam',loss='mean_squared_error',metrics=['mean_squared_error'])
print(mod.summary())
return mod
if __name__ == '__main__':
print("python screen-detetcion-5-nn.py trainData.npz 10 testVideo.mp4")
TRAINING_DATA=sys.argv[1]
EPOCHS=int(sys.argv[2])
TEST_VIDEO=sys.argv[3]
data=np.load(TRAINING_DATA)
X=data['X'].astype(np.float32)
Y=data['Y'].astype(np.float32)
print(Y)
data=None
Y[:,:,0]=Y[:,:,0]/1080.0
Y[:,:,1]=Y[:,:,1]/1920.0
X=X/256.0
Y=np.reshape(Y,newshape=(Y.shape[0],8))
print(Y)
if DEBUG: print("Shapes X={} Y={}".format(X.shape,Y.shape))
if DEBUG: print("X=[{},{}] Y=[{},{}]".format(np.min(X),np.max(X),np.min(Y),np.max(Y)))
mod=getModel(X.shape[1:],Y.shape[1:])
mod.fit(x=X,y=Y,batch_size=4,epochs=EPOCHS,validation_split=0.0,shuffle=True)
yPred=mod.predict(X)
#>>>>>>>>>>Inverting
Y=Y.reshape((Y.shape[0],4,2))
Y[:,:,0]=Y[:,:,0]*1080.0
Y[:,:,1]=Y[:,:,1]*1920.0
X=X*256.0
X=X.astype(np.uint8)
yPred=yPred.reshape((yPred.shape[0],4,2))
yPred[:,:,0]=yPred[:,:,0]*1080.0
yPred[:,:,1]=yPred[:,:,1]*1920.0
yPred=yPred.astype(np.uint)
#<<<<<<<<<<<<<<Inverting over
#yPred=Y
for i in range(yPred.shape[0]):
if DEBUG:
print("Y, yPred")
# print(X[i])
print(Y[i])
print(yPred[i])
frame=np.copy(X[i])
for j in range(yPred.shape[1]):
cv2.circle(frame,center=(yPred[i,j,0],yPred[i,j,1]),radius=3,color=[255,0,0],thickness=3)
cv2.line(frame,pt1=(yPred[i,j,0],yPred[i,j,1]),pt2=(yPred[i,(j+1)%4,0],yPred[i,(j+1)%4,1]),color=[255,255,255],thickness=1)
cv2.imshow("frame",frame)
cv2.waitKey(10)
vidIn=cv2.VideoCapture(TEST_VIDEO)
while True:
ret,frame=vidIn.read()
if not ret: break
frame=cv2.resize(frame,dsize=(X.shape[2],X.shape[1]))
yPred=mod.predict((frame.astype(np.float32)/256.0).reshape((1,X.shape[1],X.shape[2],3)))
yPred=yPred.reshape((yPred.shape[0],4,2))
yPred[:,:,0]=yPred[:,:,0]*1080.0
yPred[:,:,1]=yPred[:,:,1]*1920.0
yPred=yPred.astype(np.uint)
for j in range(yPred.shape[1]):
cv2.circle(frame,center=(yPred[0,j,0],yPred[0,j,1]),radius=3,color=[255,0,0],thickness=3)
cv2.line(frame,pt1=(yPred[0,j,0],yPred[0,j,1]),pt2=(yPred[0,(j+1)%4,0],yPred[0,(j+1)%4,1]),color=[255,255,255],thickness=1)
cv2.imshow("frame",frame)
cv2.waitKey(10)
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