Advertisement
Guest User

Untitled

a guest
Sep 19th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.81 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # coding: utf-8
  3.  
  4. target_model = "yolov3-tiny-janken_final.weights"
  5. target_config = "yolov3-tiny-janken.cfg"
  6.  
  7. import cv2 as cv
  8. import numpy as np
  9.  
  10. MODEL = "./janken_cfg/" + target_model
  11. CFG = "./janken_cfg/" + target_config
  12. SCALE = 0.00392 ##1/255
  13. INP_SHAPE = (416, 416) #input size
  14. MEAN = 0
  15. RGB = True
  16.  
  17. # Load a network
  18. net = cv.dnn.readNetFromDarknet(CFG, MODEL)
  19. net.setPreferableBackend(cv.dnn.DNN_BACKEND_DEFAULT)
  20. ##net.setPreferableTarget(cv.dnn.DNN_TARGET_CPU)
  21. net.setPreferableTarget(cv.dnn.DNN_TARGET_MYRIAD)
  22.  
  23. confThreshold = 0.8 # Confidence threshold
  24. nmsThreshold = 0.8 # Non-maximum supression threshold
  25.  
  26. class_names = ['active', 'goo', 'choki', 'pa', 'won', 'lose', 'draw']
  27.  
  28. def getOutputsNames(net):
  29. layersNames = net.getLayerNames()
  30. return [layersNames[i[0] - 1] for i in net.getUnconnectedOutLayers()]
  31.  
  32. def postprocess(frame, outs):
  33. frameHeight = frame.shape[0]
  34. frameWidth = frame.shape[1]
  35.  
  36. def drawPred(classId, conf, left, top, right, bottom):
  37. left = int(left)
  38. top = int(top)
  39. right = int(right)
  40. bottom = int(bottom)
  41. # Draw a bounding box.
  42. cv.rectangle(frame, (left, top), (right, bottom), (0, 255, 0))
  43.  
  44. label = class_names[classId] + '_%.2f' % conf
  45.  
  46. labelSize, baseLine = cv.getTextSize(label, cv.FONT_HERSHEY_SIMPLEX, 0.5, 1)
  47. top = max(top, labelSize[1])
  48. cv.rectangle(frame, (left, top - labelSize[1]), (left + labelSize[0], top + baseLine), (255, 255, 255), cv.FILLED)
  49. cv.putText(frame, label, (left, top), cv.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0))
  50.  
  51. layerNames = net.getLayerNames()
  52. lastLayerId = net.getLayerId(layerNames[-1])
  53. lastLayer = net.getLayer(lastLayerId)
  54.  
  55. classIds = []
  56. confidences = []
  57. boxes = []
  58.  
  59. if lastLayer.type == 'Region':
  60. classIds = []
  61. confidences = []
  62. boxes = []
  63. for out in outs:
  64. for detection in out:
  65. scores = detection[5:]
  66. classId = np.argmax(scores)
  67. confidence = scores[classId]
  68. if confidence > confThreshold:
  69. center_x = int(detection[0] * frameWidth)
  70. center_y = int(detection[1] * frameHeight)
  71. width = int(detection[2] * frameWidth)
  72. height = int(detection[3] * frameHeight)
  73. left = center_x - width / 2
  74. top = center_y - height / 2
  75. classIds.append(classId)
  76. confidences.append(float(confidence))
  77. boxes.append([left, top, width, height])
  78. else:
  79. print('Unknown output layer type: ' + lastLayer.type)
  80. exit()
  81.  
  82. indices = cv.dnn.NMSBoxes(boxes, confidences, confThreshold, nmsThreshold)
  83. for i in indices:
  84. i = i[0]
  85. box = boxes[i]
  86. left = box[0]
  87. top = box[1]
  88. width = box[2]
  89. height = box[3]
  90. drawPred(classIds[i], confidences[i], left, top, left + width, top + height)
  91.  
  92. c = cv.VideoCapture(0)
  93. c.set(cv.CAP_PROP_FRAME_WIDTH, 640) # カメラ画像の横幅を1280に設定
  94. c.set(cv.CAP_PROP_FRAME_HEIGHT, 480) # カメラ画像の縦幅を720に設定
  95. c.read()
  96.  
  97. r, frame = c.read()
  98.  
  99. frameHeight = frame.shape[0]
  100. frameWidth = frame.shape[1]
  101. # Create a 4D blob from a frame.
  102. inpWidth = INP_SHAPE[0]
  103. inpHeight = INP_SHAPE[1]
  104. blob = cv.dnn.blobFromImage(frame, SCALE, (inpWidth, inpHeight), MEAN, RGB, crop=False)
  105.  
  106. # Run a model
  107. net.setInput(blob)
  108. outs = net.forward(getOutputsNames(net))
  109.  
  110. ##print(outs)
  111. postprocess(frame, outs)
  112. # Put efficiency information.
  113. t, _ = net.getPerfProfile()
  114. label = 'Inference time: %.2f ms' % (t * 1000.0 / cv.getTickFrequency())
  115. cv.putText(frame, label, (0, 15), cv.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0))
  116. #対象ファイルコピー保存
  117. target_filepath = './result.jpg'
  118. cv.imwrite(target_filepath, frame)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement