Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2019
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.83 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Thu Mar 1 22:13:35 2018
  4.  
  5. @author: Marin Čereg
  6. Za Izradu sustava skupljanja loptica robotskim manipulatorom
  7. program je zamišljen raditi na Raspbery PI
  8. """
  9.  
  10. # Ucitavanje biblioteka
  11. from collections import deque
  12. import numpy as np
  13. import argparse
  14. import imutils
  15. import cv2
  16. import urllib #UCITAVANJE URL-a
  17.  
  18. Loop=1
  19.  
  20. # ARGUMENTI
  21. ap = argparse.ArgumentParser()
  22. ap.add_argument("-v", "--video",
  23. help="path to the (optional) video file")
  24. ap.add_argument("-b", "--buffer", type=int, default=64,
  25. help="max buffer size")
  26. args = vars(ap.parse_args())
  27. #SEARCH CONDITION AND CORRECTION OF WRONG INPUT
  28. print("please select color : GREEN,RED,BLUE,ALL")
  29. Search=input()
  30. while(Search!="red" and Search!="RED" and Search!="green" and Search!="GREEN" and Search!="blue" and Search!="BLUE"and Search!="ALL"and Search!="all"):
  31. Search=input()
  32.  
  33.  
  34. # COLOR SPACE AND SEARCH OPTION
  35. if (Search==("Red") or Search ==("RED")):
  36. lower = {'red':(166, 84, 141)}
  37. upper = {'red':(186,255,255)}
  38. if (Search==("blue") or Search ==("BLUE")):
  39. lower = {'blue':(97, 100, 117)}
  40. upper = {'blue':(117,255,255)}
  41. if (Search==("green") or Search ==("GREEN")):
  42. lower = {'green':(66, 122, 129)}
  43. upper = {'green':(86,255,255)}
  44. if (Search==("ALL") or Search ==("all")):
  45. lower = {'red':(166, 84, 141), 'green':(66, 122, 129), 'blue':(97, 100, 117), 'yellow':(23, 59, 119), 'orange':(0, 50, 80)}
  46. upper = {'red':(186,255,255), 'green':(86,255,255), 'blue':(117,255,255), 'yellow':(54,255,255), 'orange':(20,255,255)}
  47. # STANDRARD COLORS , LABEL
  48. colors = {'red':(0,0,255), 'green':(0,255,0), 'blue':(255,0,0)}
  49. if (Search==("ALL") or Search ==("all")):
  50. colors = {'red':(0,0,255), 'green':(0,255,0), 'blue':(255,0,0), 'yellow':(0, 255, 217), 'orange':(0,140,255)}
  51. # REFERENCA AKO NEMA VIDEO SIGNALA
  52. if not args.get("video", False):
  53. camera = cv2.VideoCapture(1)
  54. # PROGRAM
  55. else:
  56. #camera = cv2.VideoCapture(args["video"])
  57. camera = cv2.VideoCapture(1)
  58.  
  59. (grabbed, frame) = camera.read()
  60. while (Loop==1):
  61. # FRAME CAMPTURE
  62. (grabbed, frame) = camera.read()
  63. if args.get("video") and not grabbed:
  64. break
  65.  
  66. # FRAME WIDTH
  67. frame = imutils.resize(frame, width=1000)
  68. #oznaka objekta
  69. blurred = cv2.GaussianBlur(frame, (11, 11), 0)
  70. hsv = cv2.cvtColor(blurred, cv2.COLOR_BGR2HSV)
  71. #for each color in dictionary check object in frame
  72. for key, value in upper.items():
  73. kernel = np.ones((9,9),np.uint8)
  74. mask = cv2.inRange(hsv, lower[key], upper[key])
  75. mask = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel)
  76. mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel)
  77. # PRONADJI CENTAR OBJEKTA
  78. cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL,
  79. cv2.CHAIN_APPROX_SIMPLE)[-2]
  80. center = None
  81. # AKO PRONADJE BOJU
  82. if len(cnts) > 0:
  83. # pronadji kontur i napravi krug CENTROID
  84. c = max(cnts, key=cv2.contourArea)
  85. ((x, y), radius) = cv2.minEnclosingCircle(c)
  86. M = cv2.moments(c)
  87. center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"]))
  88. # only proceed if the radius meets a minimum size. Correct this value for your obect's size
  89. if radius > 0.5:
  90. # NACRTAJ KRUG I UPDEJTAJ FRAME
  91. # FILTRIRAJ SCREEN
  92. cv2.circle(frame, (int(x), int(y)), int(radius), colors[key], 2)
  93. cv2.putText(frame,key , (int(x-radius),int(y-radius)), cv2.FONT_HERSHEY_SIMPLEX, 0.6,colors[key],2)
  94. print(key)
  95. print("X-os",x,"_____","Y-os",y)
  96.  
  97. # SHOW FRAME
  98. cv2.imshow("Frame", frame)
  99. Loop=2
  100.  
  101. # CLEANUP
  102. cv2.waitKey(0)
  103. cv2.destroyAllWindows()
  104. camera.release()
  105. cv2.destroyAllWindows()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement