Commit 0b2977e3 authored by Gihan Jayatilaka's avatar Gihan Jayatilaka

sc-detect-6

parent 67402f8c
from sklearn.ensemble import RandomForestClassifier
DEBUG=True
CUDA_DEV=0
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
import cv2
import sys
import os
from keras import layers
from keras.models import load_model
from keras.layers import *
from keras import Model, Sequential
from keras.activations import relu,sigmoid
def conv2d_bn(x,
filters,
num_row,
num_col,
padding='same',
strides=(1, 1),
name=None):
if name is not None:
bn_name = name + '_bn'
conv_name = name + '_conv'
else:
bn_name = None
conv_name = None
if K.image_data_format() == 'channels_first':
bn_axis = 1
else:
bn_axis = 3
x = Conv2D(
filters, (num_row, num_col),
strides=strides,
padding=padding,
use_bias=False,
name=conv_name)(x)
x = BatchNormalization(axis=bn_axis, scale=False, name=bn_name)(x)
x = Activation('relu', name=name)(x)
return x
# In[ ]:
def inceptionBlockA(x,noKernels):
branch1x1 = conv2d_bn(x, 64, 1, 1)
branch5x5 = conv2d_bn(x, 48, 1, 1)
branch5x5 = conv2d_bn(branch5x5, 64, 5, 5)
branch3x3dbl = conv2d_bn(x, 64, 1, 1)
branch3x3dbl = conv2d_bn(branch3x3dbl, 96, 3, 3)
branch3x3dbl = conv2d_bn(branch3x3dbl, 96, 3, 3)
branch_pool = AveragePooling2D((3, 3), strides=(1, 1), padding='same')(x)
branch_pool = conv2d_bn(branch_pool, noKernels, 1, 1)
x = layers.concatenate([branch1x1, branch5x5, branch3x3dbl, branch_pool],axis=3)#,name='mixed0{}'.format(l))
return x
def inceptionConeA(img_input):
x = conv2d_bn(img_input, 32, 3, 3, strides=(2, 2), padding='valid')
x = conv2d_bn(x, 32, 3, 3, padding='valid')
x = conv2d_bn(x, 64, 3, 3)
x = MaxPooling2D((3, 3), strides=(2, 2))(x)
x = conv2d_bn(x, 80, 1, 1, padding='valid')
x = conv2d_bn(x, 192, 3, 3, padding='valid')
x = MaxPooling2D((3, 3), strides=(2, 2))(x)
return x
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'])
'''
Very shallow inception
'''
imgInput = Input(shape=xShape)
y=inceptionConeA(imgInput)
y=inceptionBlockA(y,64)
y = layers.AveragePooling2D(name='avg_pool')(y)
y = Flatten()(y)
y = Dropout(0.5)(y)
#y = Dense(32, activation='relu', name='extra-dense')(y)
y = Dense(8, activation='linear', name='predictions')(y)
mod=Model(imgInput,y)
from keras.optimizers import rmsprop
opt = rmsprop(lr=0.0001, decay=1e-6)
mod.compile(loss='mean_squared_error',
optimizer=opt,
metrics=['mean_squared_error','accuracy'])
print(mod.summary())
return mod
def transformY(Y):
Y=Y.astype(np.float32)
param1=1080.0
param2=1920.0
Y[:,:,0]=Y[:,:,0]/param2
Y[:,:,1]=Y[:,:,1]/param1
Y=np.reshape(Y,newshape=(Y.shape[0],8))
return Y,[param1,param2]
def inverseTransformY(Y,params):
param1=params[0]
param2=params[1]
Y=Y.reshape((Y.shape[0],4,2))
Y[:,:,0]=Y[:,:,0]*299.0
Y[:,:,1]=Y[:,:,1]*299.0
return Y.astype(np.int32)
def load(fileName):
data=np.load(fileName)
X=data['X']
temp=np.ndarray(shape=(X.shape[0],299,299,3),dtype=np.uint8)
for i in range(X.shape[0]):
temp[i]=cv2.resize(X[i],dsize=(299,299))
X=temp
Y=data['Y']
data=None
return X,Y
def transformX(X):
X=(X.astype(np.float32)/128.0)-1
return X
def inverseTransformX(X):
return ((X+1.0)*128.0).astype(np.uint8)
if __name__ == '__main__':
print("python screen-detetcion-5-nn.py trainData.npz 10 testVideo.mp4")
print("python screen-detetcion-5-nn.py trainData.npz 10 testVideo.mp4 modelSave.h5 0")
TRAINING_DATA=sys.argv[1]
EPOCHS=int(sys.argv[2])
TEST_VIDEO=sys.argv[3]
MODEL_FILE_LOCATION=sys.argv[4]
CUDA_DEV=sys.argv[5]
X,Y=load(TRAINING_DATA)
os.environ["CUDA_VISIBLE_DEVICES"]="{}".format(CUDA_DEV)
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)))
if DEBUG: print("DEBUG: Resized X shape={}".format(X.shape))
if DEBUG: print("Non-normalized X=[{},{}] Y=[{},{}]".format(np.min(X),np.max(X),np.min(Y),np.max(Y)))
X=transformX(X)
Y,yParams=transformY(Y)
if DEBUG: print("Shapes X={} Y={}".format(X.shape,Y.shape))
if DEBUG: print("Normalized 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)
mod=None
try:
mod = load_model(MODEL_FILE_LOCATION)
except:
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)
mod.save(MODEL_FILE_LOCATION)
print("DEBUG: FINISHED SAVING MODEL as {}".format(MODEL_FILE_LOCATION))
yPred=mod.predict(X)
yPred=inverseTransformY(yPred,yParams)
Y=inverseTransformY(Y,yParams)
X=inverseTransformX(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
# if DEBUG: print("Y shape {}. yPred shape {}".format(yPred.shape,Y.shape))
# 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]):
#Predicted
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.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,0,0],thickness=1)
#True
cv2.circle(frame,center=(Y[i,j,0],Y[i,j,1]),radius=3,color=[255,255,255],thickness=2)
cv2.line(frame,pt1=(Y[i,j,0],Y[i,j,1]),pt2=(Y[i,(j+1)%4,0],Y[i,(j+1)%4,1]),color=[255,255,255],thickness=1)
cv2.imshow("frame",frame)
cv2.waitKey(10)
......@@ -104,14 +215,15 @@ if __name__ == '__main__':
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)))
frame=cv2.resize(frame,dsize=(299,299))
frame=np.reshape(frame,newshape=(1,299,299,3))
frame=transformX(frame)
yPred=mod.predict(frame)
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)
yPred=inverseTransformY(yPred,yParams)
frame=inverseTransformX(frame)
frame=np.reshape(frame,newshape=(299,299,3))
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)
......
import cv2
import numpy as np
from scipy import ndimage
from scipy.signal import convolve2d
import os
import sys
import random
import time
WIDTH = 1920
HEIGHT = 1080
NUM_FRAME_PER_VIDEO = 2000
GRID_X = 20
GRID_Y = 20
GAUSS = 0
DELTA = 5
print("python screen-detection-6-dataset-prep.py ./video/video-and-green.mp4 ./dataset-screen-detection.npz")
INPUT_FILE_NAME = sys.argv[1]
SAVE_FILE_NAME= sys.argv[2]
video = cv2.VideoCapture(INPUT_FILE_NAME)
corners=[]
isVideo=[]
isGreen=[]
ret, frame = video.read()
xMid=int(frame.shape[1]/2)
yMid=int(frame.shape[0]/2)
while video.isOpened():
ret, frame = video.read()
if ret:
lower = np.array([50, 80, 100])
upper = np.array([70, 255, 255])
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
mask = cv2.inRange(hsv, lower, upper)
contours, hierarchy = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
areas = [cv2.contourArea(c) for c in contours]
if len(contours)>0:
max_cnt=contours[np.argmax(areas)]
m=np.reshape(max_cnt,(max_cnt.shape[0],2))
maxArea=np.max(areas)
isVideo.append(maxArea<frame.shape[0]*frame.shape[1]/16.0)
isGreen.append(maxArea>frame.shape[0]*frame.shape[1]/4.0)
if isGreen[-1]:
M = cv2.moments(max_cnt)
xMid = int(M["m10"] / M["m00"])
yMid = int(M["m01"] / M["m00"])
temp=np.square(m[:,0]-xMid)+np.square(m[:,1]-yMid)
lt=np.argmax((m[:,0]<xMid).astype(np.float32)*(m[:,1]<yMid).astype(np.float32)*temp)
rt=np.argmax((m[:,0]>xMid).astype(np.float32)*(m[:,1]<yMid).astype(np.float32)*temp)
rd=np.argmax((m[:,0]>xMid).astype(np.float32)*(m[:,1]>yMid).astype(np.float32)*temp)
ld=np.argmax((m[:,0]<xMid).astype(np.float32)*(m[:,1]>yMid).astype(np.float32)*temp)
corners.append(np.array([m[lt],m[rt],m[rd],m[ld]]))
cv2.circle(frame,center=(xMid,yMid),radius=20,color=[255,0,0],thickness=10)
else:
corners.append(np.zeros((4,2)))
if isVideo[-1]:
cv2.drawContours(frame, max_cnt, -1, (0,0,255), 5)
cv2.circle(frame,center=(xMid,yMid),radius=300,color=[0,0,255],thickness=6)
elif isGreen[-1]:
cv2.drawContours(frame, max_cnt, -1, (255,0,0), 5)
else:
cv2.drawContours(frame, max_cnt, -1, (0,255,0), 5)
else:
corners.append(np.zeros((4,2)))
isVideo.append(True)
isGreen.append(False)
cv2.circle(frame,center=(xMid,yMid),radius=300,color=[0,0,255],thickness=6)
cv2.imshow("frame", cv2.resize(frame, (500,500)))
cv2.waitKey(20)
# if cv2.waitKey() & 0xFF == ord('q'):
# break
else:
corners=np.array(corners).astype(np.uint)
isVideo=np.array(isVideo)
isGreen=np.array(isGreen)
break
video.release()
cv2.destroyAllWindows()
video = cv2.VideoCapture(INPUT_FILE_NAME)
X=[]
Y=[]
ret,frame=video.read()
ret,frame=video.read()
for i in range(len(corners)-1):
ret,frame=video.read()
if isGreen[i] and isVideo[i+1]:
X.append(frame)
Y.append(corners[i])
frameCopy=np.copy(frame)
yPred=corners[i]
for j in range(4):
cv2.circle(frameCopy,center=(yPred[j,0],yPred[j,1]),radius=3,color=[255,0,0],thickness=3)
cv2.line(frameCopy,pt1=(yPred[j,0],yPred[j,1]),pt2=(yPred[(j+1)%4,0],yPred[(j+1)%4,1]),color=[255,255,255],thickness=1)
cv2.imshow("frame",cv2.resize(frameCopy, (500, 500)))
cv2.waitKey(100)
X=np.array(X)
Y=np.array(Y)
np.savez(SAVE_FILE_NAME,X=X,Y=Y)
cv2.destroyAllWindows()
video.release()
print("Finished saving file {} with xShape {} and yShape {}".format(SAVE_FILE_NAME,X.shape,Y.shape))
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