def getAttributesFromNetwork(self,Network,image): # The arrays above will hold the attribute information for the Network ShapeList = [] AngleList = [] FillList = [] SizeList = [] # The scale will be 1-5; with 5 being the biggest AdditionList = [] print Network.letter # Open the image path from the Problems (Image Data) folder image = Image.open(image) # Convert it to grayscale for easy processing grayscale = image.convert("L") # Convert it to black and white to ensure that there aren't any pixels of any # other color. blackwhite = grayscale.point(self.filter,"1") image = blackwhite image = image.convert("RGB") width,height = image.size colorindex = 0 # Translate the image into pictures with various colors while True: color = DISTINCT_COLORS[colorindex] colorindex += 1 blackpixel = None for x,y,pixel in self.walk(image): if pixel == (0,0,0): blackpixel = (x,y) break if not blackpixel: break neighbors = [blackpixel] while len(neighbors) > 0: processing = list(neighbors) neighbors = [] for x,y in processing: image.putpixel((x,y),color) new = [(x-1,y),(x+1,y),(x,y-1),(x,y+1)] for x,y, in new: if (x,y) in neighbors: continue if x < 0 or x >= width: continue if y < 0 or y >= height: continue if image.getpixel((x,y)) != (0,0,0): continue neighbors.append((x,y)) # We use the count to save each network as a different image self.count = str(self.count) # Save the network image image.save("Project4/ImagesForProcessing/colored"+self.count+".png") # Open the network image; here, we'll convert it to a bunch of different # images with different shapes representing the networks im = Image.open("Project4/ImagesForProcessing/colored"+self.count+".png") # Separate the images colors_dict = color_separator(im) imageCount = 0 # Iterate through the color dictionary for all of the images for key,value in colors_dict.iteritems(): if key == (255, 255, 255): imageCount += 1 continue imageCount = str(imageCount) # grab the individual image, # save it, value.save("Project4/ImagesForProcessing/"+Network.letter+"coloredSmall"+imageCount+".png") # then read it back with OpenCV for processing img = cv2.imread("Project4/ImagesForProcessing/"+Network.letter+"coloredSmall"+imageCount+".png") # Convert it to grayscale; it processes better this way imgray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) ret,thresh = cv2.threshold(imgray,127,255,0) # find the contours in the image contours,hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) # iterate through the contours, #print "Contour Length: ",len(contours) #if not contours: #print "No Contours" for cnt in contours: if (len(contours)) == 1: FillList.append("yes") else: FillList.append("no") #print "Looking through contours" #if (count%2) == 1: #count = count + 1 #print "Count2: ",count #continue # approximate how many sides it has approx = cv2.approxPolyDP(cnt,0.01*cv2.arcLength(cnt,True),True) print len(approx) area = cv2.contourArea(cnt) if len(approx) == 5: print "Half-Arrow" ShapeList.append("Half-Arrow") if len(approx) == 7: print "Arrow" ShapeList.append("Arrow") elif len(approx) == 3: print "Triangle" ShapeList.append("Triangle") elif len(approx) == 4: print "Square" ShapeList.append("Square") elif len(approx) == 12 and area >= 1000: print "Cross" ShapeList.append("Cross") elif len(approx) >= 8: print "Circle" ShapeList.append("Circle") #(x,y),(MA,ma),angle = cv2.fitEllipse(cnt) #AngleList.append(angle) #count = count + 1 #print "Count: ",count3 area = cv2.contourArea(cnt) SizeList.append(self.checkArea(area)) print "Area: ",area,"\n" if len(approx) == 12 and area >= 1000: (x,y),(MA,ma),angle = cv2.fitEllipse(cnt) print "Angle: ",round(angle,0) AngleList.append(round(angle,0)) else: M = cv2.moments(cnt) angle = self.checkAngle(M) print "Angle: ",round(angle,0) AngleList.append(round(angle,0)) break #cv2.imshow("img",img) #cv2.waitKey(0) #cv2.destroyAllWindows() imageCount = int(imageCount) imageCount += 1 self.count = int(self.count) self.count = self.count + 1 Network.ObjectNames["Shape"] = ShapeList Network.ObjectNames["Angle"] = AngleList Network.ObjectNames["Objects"] = colorindex-1 Network.ObjectNames["Fill"] = FillList Network.ObjectNames["Size"] = SizeList print "Shapes: ",Network.ObjectNames["Shape"] print "Angles: ",Network.ObjectNames["Angle"] print "Fill: ",Network.ObjectNames["Fill"] print "Size: ",Network.ObjectNames["Size"] print "Objects: ",Network.ObjectNames["Objects"],"\n"
BAD_PACKET = False IMG_BUFFER = BytesIO() if NEXT_PACKET != SEQ_NUM: BAD_PACKET = True NEXT_PACKET = 0 syslog.syslog(syslog.LOG_DEBUG, "Missed packet sequence number."\ + "Expected %s, got %s" % (NEXT_PACKET, SEQ_NUM)) if binascii.crc32(PAYLOAD) != CRC_32: BAD_PACKET = True syslog.syslog(syslog.LOG_DEBUG, "Bad PAYLOAD CRC in packet") if BAD_PACKET == False and NEXT_PACKET == SEQ_NUM: for i in range(0, BYTES_SENT): IMG_BUFFER.write(list(PAYLOAD)[i]) NEXT_PACKET += 1 if LAST_PKT == True and BAD_PACKET == False: for proc in psutil.process_iter(): if proc.name == "display": proc.kill() NEXT_PACKET = 0 FILE_NAME = str(int(time.time())) + '.jpg' JPG_FILE = open(FILE_NAME, 'wb') JPG_FILE.write(IMG_BUFFER.getvalue()) JPG_FILE.close() Image.open(StringIO(IMG_BUFFER.getvalue())).show() elif LAST_PKT == True and BAD_PACKET == True: NEXT_PACKET = 0 BAD_PACKET = False syslog.syslog(syslog.LOG_ERR, "Received last packet afer bad packets. Resetting") >>>>>>> 978c811362e9a2947741249d19bac3a7a378b04a