Guest User

Untitled

a guest
Jan 19th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.90 KB | None | 0 0
  1. import os
  2. import logging as log
  3. import matplotlib.pyplot as plt
  4. from PIL import Image
  5. import numpy as np
  6. import tensorflow as tf
  7. import cv2
  8. import re
  9. import tqdm
  10. import keras
  11. from keras.layers import Dense, Conv2D, Dropout, MaxPooling2D, Flatten
  12. from keras.models import Sequential,load_model
  13. from keras.callbacks import ModelCheckpoint,EarlyStopping, TensorBoard
  14. video = cv2.VideoCapture(1)
  15.  
  16. while True:
  17. X_test = []
  18. flag,frame = video.read()
  19. #image = cv2.imread('/home/tpemist/Desktop/visualtest/train/info/0001_0010_0035_0054_0054.jpg')
  20. show = cv2.resize(frame,(640,480))
  21. image = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
  22. image = cv2.resize(image,(128,96))
  23. nparr = np.array(image) / 255.0
  24. X_test.append(nparr)
  25. X_test = np.array(X_test, dtype=np.float32)
  26. X_test = X_test.reshape(X_test.shape[0], 96, 128, 1)
  27.  
  28. model = Sequential()
  29.  
  30. model.add(Conv2D(32, (3, 3),padding = 'same', activation = 'relu', input_shape = (96,128,1)))
  31. model.add(MaxPooling2D((2,2)))
  32. model.add(Dropout(0.5))
  33. model.add(Conv2D(32, (3, 3),padding = 'same', activation = 'relu'))
  34. model.add(Conv2D(32, (3, 3),padding = 'same', activation = 'relu'))
  35. model.add(MaxPooling2D((2,2)))
  36. model.add(Dropout(0.5))
  37. model.add(Conv2D(32, (3, 3),padding = 'same', activation = 'relu'))
  38. model.add(MaxPooling2D((2,2)))
  39. model.add(Dropout(0.75))
  40.  
  41. model.add(Flatten())
  42. model.add(Dense(200))
  43. model.add(Dropout(0.5))
  44. model.add(Dense(200))
  45. model.add(Dropout(0.5))
  46. model.add(Dense(3))
  47.  
  48. X_test = np.array(X_test)
  49. model.load_weights('train_model2')
  50. location = model.predict(X_test)
  51. location = location.astype(np.float32)
  52. x = (location[0])[0]
  53. y = (location[0])[1]
  54. w = (location[0])[2]
  55. #print x,y,w
  56. x1 = int((x*5+(x-1)*5)/2)
  57. y2 = int((y*5+(y-1)*5)/2)
  58. w2 = int(w*2.5)
  59. #print x1,y2,w2
  60. cv2.rectangle(show, (x1,y2), (x1+w2, y2+w2), (255, 255, 255), 2)
  61.  
  62. cv2.imshow('Video', show)
  63. if cv2.waitKey(1) & 0xFF == ord('q'):
  64. break
  65.  
  66.  
  67.  
  68. video.release()
  69. cv2.destroyAllWindows()
Add Comment
Please, Sign In to add comment