Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2019
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.59 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. #from Baxter import *
  4.  
  5. import sys
  6. import cv2
  7. import os
  8. #from sensor_msgs.msg import Image
  9. #from cv_bridge import CvBridge, CvBridgeError
  10.  
  11. from shapedetector import ShapeDetector
  12. import imutils
  13. """
  14. from geometry_msgs.msg import (
  15. PoseStamped,
  16. Pose,
  17. Point,
  18. Quaternion,
  19. )
  20. """
  21.  
  22. img_format = ".jpg"
  23.  
  24.  
  25. #baxter = Baxter_Robot("right")
  26.  
  27. #"Corner\nx=0.472849478978 y=0.295796754786 z=-0.173375298769 x=0.342246552482 y=0.93797464198 z=0.044132681924 w=-0.0335137986772\n"
  28. """
  29. pose = Pose()
  30.  
  31. pose.position.x = 0.4
  32. pose.position.y = 0.
  33. pose.position.z = 0.6
  34. pose.orientation.x = 1.
  35. pose.orientation.y = 0.
  36. pose.orientation.z = 0.
  37. pose.orientation.w = -1.
  38.  
  39. joint_angles = baxter.ik_request(pose)
  40.  
  41. #baxter.move_to_joint_angles(joint_angles)
  42.  
  43.  
  44. #joint_angles['right_w2'] -= 2.5
  45.  
  46. #baxter.move_to_joint_angles(joint_angles)
  47.  
  48. #baxter.close_grippers()
  49.  
  50. """
  51.  
  52. image_topic = "/cameras/" + "right_hand_camera" + "/image"
  53. path=os.path.expanduser('~/FER/proba/')
  54. img_name = "green2"
  55. if not os.path.exists(path):
  56. os.makedirs(path)
  57.  
  58. img_path = path + img_name + img_format
  59. """
  60. bridge = CvBridge()
  61.  
  62. msg = rospy.wait_for_message(image_topic, Image)
  63. try:
  64. cv2_img = bridge.imgmsg_to_cv2(msg, "bgr8")
  65. except CvBridgeError, e:
  66. print(e)
  67.  
  68. cv2.imwrite(img_path, cv2_img)
  69. """
  70. #take_screenshot(index="_01")
  71.  
  72.  
  73. image = cv2.imread(img_path)
  74. resized = imutils.resize(image, width=300)
  75. ratio = image.shape[0] / float(resized.shape[0])
  76.  
  77. # convert the resized image to grayscale, blur it slightly,
  78. # and threshold it
  79. gray = cv2.cvtColor(resized, cv2.COLOR_BGR2GRAY)
  80. blurred = cv2.GaussianBlur(gray, (5, 5), 0)
  81. thresh = cv2.threshold(blurred, 60, 255, cv2.THRESH_BINARY)[1]
  82.  
  83. # find contours in the thresholded image and initialize the
  84. # shape detector
  85. cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,
  86. cv2.CHAIN_APPROX_SIMPLE)
  87. cnts = imutils.grab_contours(cnts)
  88. sd = ShapeDetector()
  89. print(cnts)
  90.  
  91. for c in cnts:
  92. # compute the center of the contour, then detect the name of the
  93. # shape using only the contour
  94. M = cv2.moments(c)
  95. cX = int((M["m10"] / M["m00"]) * ratio)
  96. cY = int((M["m01"] / M["m00"]) * ratio)
  97. shape = sd.detect(c)
  98.  
  99. # multiply the contour (x, y)-coordinates by the resize ratio,
  100. # then draw the contours and the name of the shape on the image
  101. c = c.astype("float")
  102. c *= ratio
  103. c = c.astype("int")
  104. cv2.drawContours(image, [c], -1, (0, 255, 0), 2)
  105. cv2.putText(image, shape, (cX, cY), cv2.FONT_HERSHEY_SIMPLEX,
  106. 0.5, (255, 255, 255), 2)
  107.  
  108. # show the output image
  109. cv2.imshow("Image", image)
  110. cv2.waitKey(500)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement