Advertisement
Guest User

Untitled

a guest
Jan 16th, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.13 KB | None | 0 0
  1. # USAGE
  2. # python real_time_object_detection.py --prototxt MobileNetSSD_deploy.prototxt.txt --model MobileNetSSD_deploy.caffemodel
  3.  
  4. # import the necessary packages
  5. import win32api
  6.  
  7. import keyboard as keyboard
  8. import pygame as pygame
  9. import pythoncom
  10. import win32con
  11. from PIL import ImageGrab
  12. from imutils.video import VideoStream
  13. from imutils.video import FPS
  14. import numpy as np
  15. import argparse
  16. import imutils
  17. import time
  18. import cv2
  19. import pyautogui
  20.  
  21.  
  22. # construct the argument parse and parse the arguments
  23. from keyboard._mouse_event import RIGHT
  24.  
  25. ap = argparse.ArgumentParser()
  26. ap.add_argument("-p", "--prototxt", required=False,
  27. help="path to Caffe 'deploy' prototxt file")
  28. ap.add_argument("-m", "--model", required=False,
  29. help="path to Caffe pre-trained model")
  30. ap.add_argument("-c", "--confidence", type=float, default=0.6,
  31. help="minimum probability to filter weak detections")
  32. args = vars(ap.parse_args())
  33. prott1 = 'C:\Users\Saehi\PycharmProjects/testing\MobileNetSSD_deploy.prototxt.txt'
  34. prott2 = 'C:\Users\Saehi\PycharmProjects/testing\MobileNetSSD_deploy.caffemodel'
  35. # initialize the list of class labels MobileNet SSD was trained to
  36. # detect, then generate a set of bounding box colors for each class
  37. CLASSES = ["background", "aeroplane", "bicycle", "bird", "boat",
  38. "bottle", "bus", "car", "cat", "chair", "cow", "diningtable",
  39. "dog", "horse", "motorbike", "person", "pottedplant", "sheep",
  40. "sofa", "train", "tvmonitor"]
  41. COLORS = np.random.uniform(0, 255, size=(len(CLASSES), 3))
  42.  
  43. # load our serialized model from disk
  44. print("[INFO] loading model...")
  45. net = cv2.dnn.readNetFromCaffe(prott1, prott2)
  46.  
  47. # initialize the video stream, allow the cammera sensor to warmup,
  48. # and initialize the FPS counter
  49. print("[INFO] starting video stream...")
  50. #vs = VideoStream(src=0).start()
  51. #time.sleep(2.0)
  52. #fps = FPS().start()
  53.  
  54. # loop over the frames from the video stream
  55. HSX = 100;
  56. LSX = 1000;
  57. HSY = 100;
  58. LSY = 1000;
  59. HEX = 100;
  60. LEX = 1000;
  61. HEY = 100;
  62. LEY = 1000;
  63.  
  64. while True:
  65. # grab the frame from the threaded video stream and resize it
  66. # to have a maximum width of 400 pixels
  67.  
  68. frame = np.array(ImageGrab.grab(bbox=(0, 40, 1820, 1240)))
  69. # frame = imutils.resize(frame, width=400)
  70.  
  71. # grab the frame dimensions and convert it to a blob
  72. (h, w) = frame.shape[:2]
  73. blob = cv2.dnn.blobFromImage(cv2.resize(frame, (300, 300)),
  74. 0.007843, (300, 300), 127.5)
  75.  
  76. # pass the blob through the network and obtain the detections and
  77. # predictions
  78. net.setInput(blob)
  79. detections = net.forward()
  80.  
  81. # loop over the detections
  82. for i in np.arange(0, detections.shape[2]):
  83. # extract the confidence (i.e., probability) associated with
  84. # the prediction
  85. confidence = detections[0, 0, i, 2]
  86.  
  87. # filter out weak detections by ensuring the confidence is
  88. # greater than the minimum confidence
  89. if confidence > args["confidence"]:
  90. # extract the index of the class label from the
  91. # detections, then compute the (x, y)-coordinates of
  92. # the bounding box for the object
  93. idx = int(detections[0, 0, i, 1])
  94. box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
  95. (startX, startY, endX, endY) = box.astype("int")
  96.  
  97. # draw the prediction on the frame
  98. label = "{}: {:.2f}%".format(CLASSES[idx],
  99. confidence * 100)
  100. cv2.rectangle(frame, (startX, startY), (endX, endY),
  101. COLORS[idx], 2)
  102. y = startY - 15 if startY - 15 > 15 else startY + 15
  103. cv2.putText(frame, label, (startX, y),
  104. cv2.FONT_HERSHEY_SIMPLEX, 0.5, COLORS[idx], 2)
  105. if 'person' in label:
  106. pygame.init()
  107. pygame.event.get()
  108. if pygame.mouse.get_pressed():
  109. print 'pressing'
  110. #tried to detect my character's offset and add the best way to exclude it, failed most tests.
  111. if startX > 369 & startX < 1402 & startY > -1 & startY < 725 & endX > 339 & endX < 1805 & endY > 806 & endY < 1017:
  112. print 'found myself'
  113. else:
  114. #print 'found somebody else'
  115. nosum = int(round(startX * 1)) + int(round(startX * 0.06))
  116. nosum2 = int(round(y * 1)) + int(round(y * 0.7))
  117. halfX = (endX - startX) / 2
  118. halfY = (endY - startY) / 2
  119. finalX = startX + halfX
  120. finalY = startY + halfY
  121. pyautogui.moveTo(finalX, finalY)
  122. #win32api.SetCursorPos((finalX, finalY))
  123. win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, finalX, finalY, 0, 0)
  124. win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, finalX, finalY, 0, 0)
  125. #print 'Pressed L'
  126. if 'HSX' not in locals():
  127. HSX = startX
  128. if 'LSX' not in locals():
  129. LSX = startX
  130. if 'HSY' not in locals():
  131. HSY = startY
  132. if 'LSY' not in locals():
  133. LSY = startY
  134.  
  135.  
  136. if 'HEX' not in locals():
  137. HEX = endX
  138. if 'LEX' not in locals():
  139. LEX = endX
  140. if 'HEY' not in locals():
  141. HEY = endY
  142. if 'LEY' not in locals():
  143. LEY = endY
  144.  
  145. if startX > HSX:
  146. HSX = startX
  147. if startX < LSX:
  148. LSX = startX
  149. if startY > HSY:
  150. HSY = startY
  151. if startY < LSY:
  152. LSY = startY
  153.  
  154. if endX > HEX:
  155. HEX = endX
  156. if endX < LEX:
  157. LEX = endX
  158. if endY > HEY:
  159. HEY = endY
  160. if endY < LEY:
  161. LEY = endY
  162.  
  163. print 'HStartX: ' + str(HSX)
  164. print 'LStartX: ' + str(LSX)
  165. print 'HStartY: ' + str(HSY)
  166. print 'LStartY: ' + str(LSY)
  167. print 'HendX: ' + str(HEX)
  168. print 'LendX: ' + str(LEX)
  169. print 'HendY: ' + str(HEY)
  170. print 'LendY: ' + str(LEY)
  171.  
  172. #print args["confidence"]
  173.  
  174. # click(10,10)
  175.  
  176.  
  177. # show the output frame
  178. cv2.imshow("Frame", frame)
  179. key = cv2.waitKey(1) & 0xFF
  180.  
  181. # if the q key was pressed, break from the loop
  182. if key == ord("q"):
  183. break
  184.  
  185. # update the FPS counter
  186.  
  187.  
  188. # stop the timer and display FPS information
  189.  
  190.  
  191.  
  192. # do a bit of cleanup
  193. cv2.destroyAllWindows(
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement