Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
If statement
#1
Hey,
I've been trying to create a code that is able to detect the shape of objects and determine their corinations.
and depending on the shape of each object I want to make certain operation!
but I havn't been able to write the appropriate code to do so, I have a syntax problem with the if statement.
You'll find below my code.
Thank u for ur help
# import the necessary packages
from pyimagesearch.shapedetector import ShapeDetector
import argparse
import imutils
import cv2

# load the image and resize it to a smaller factor so that

image = cv2.imread("rectangle.png")
#image = cv2.imread("shapes1.png")
resized = imutils.resize(image, width=300)
ratio = image.shape[0] / float(resized.shape[0])

# convert the resized image to grayscale, blur it slightly,
# and threshold it
gray = cv2.cvtColor(resized, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
thresh = cv2.threshold(blurred, 60, 255, cv2.THRESH_BINARY)[1]

# find contours in the thresholded image and initialize the
# shape detector
cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,
	cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if imutils.is_cv2() else cnts[1]
sd = ShapeDetector()

# loop over the contours
for c in cnts:
    cX =0
	
    cY = 0
    Shapes= ['rectangle', 'triangle', 'circle', 'polygone']
	# compute the center of the contour, then detect the name of the
	# shape using only the contour
	
    M = cv2.moments(c)
    cX = int((M["m10"] / M["m00"]) * ratio)
    cY = int((M["m01"] / M["m00"]) * ratio)
    #print(shape)
    print('cX=',cX)
    print('cY=',cY)
    shape = sd.detect(c)
    # multiply the contour (x, y)-coordinates by the resize ratio,
	# then draw the contours and the name of the shape on the image
    c = c.astype("float")
    c *= ratio
    c = c.astype("int")

    
    #Let (x,y) be the top-left coordinate of the rectangle and (w,h) be its width and height.
    if (shape = rectangle )
    x,y,w,h = cv2.boundingRect(c)
    cv2.rectangle(image,(x,y),(x+w,y+h),(0,255,0),2)
    print('rX=',x)
    print('rY=',y)
    print('rW=',w)
    print('rH=',h)
    
    cv2.drawContours(image, [c], -1, (0, 255, 0), 2)
    cv2.putText(image, shape, (cX, cY), cv2.FONT_HERSHEY_SIMPLEX,
                0.5, (255, 255, 255), 2)
    # draw the contour and center of the shape on the image
    cv2.drawContours(image, [c], -1, (0, 255, 0), 2)
    cv2.circle(image, (cX, cY), 7, (255, 255, 255), -1)
    cv2.putText(image,"center", (cX - 20, cY - 20), ###
                cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2)
	# show the output image
cv2.imshow("Image", image)
Sorry dumb question, I should've typed it this way:
 if shape == "rectangle" :
        print('Rectangle')
        x,y,w,h = cv2.boundingRect(c)
        cv2.rectangle(image,(x,y),(x+w,y+h),(0,255,0),2)
        print('rX=',x)
        print('rY=',y)
        print('rW=',w)
        print('rH=',h)
Reply
#2
you need to take some time and get your code tags straightened out
you've got error tags in the middle of code, etc.

Very difficult to view without proper tags see: BBCODE
Reply
#3
hey;

my code is straightened, but when copy paste it, it seemed like that.
Sry I didn't pay attention before posting it.

Thank u
Reply
#4
(Apr-21-2018, 10:18 AM)Akhou Wrote: my code is straightened, but when copy paste it, it seemed like that.
You most read the link poster bye @Larz60+ about code tag BBCode.
I have added code tag in your post.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020