def main(): """ Run main program loop. Detect changes to parking spaces and update appropriate availabity of the spaces to the server. """ # setup camera and image save location camera = imageread.setup_camera() camera.start_preview() image_location = "./images/pipark.jpeg" # load data sets and count num spaces on control boxes space_boxes, control_boxes = __setup_box_data() num_spaces = len(space_boxes) num_controls = len(control_boxes) print "Number of spaces:", num_spaces, "Number of Control boxes:", num_controls assert num_spaces > 0 assert num_controls == 3 last_status = [None for i in range(10)] last_ticks = [3 for i in range(10)] # run centralised program loop while True: space_averages = [] control_averages = [] # take new picture, save to specified location camera.capture(image_location) print "INFO: New picture taken,", image_location # load image try: image = imageread.Image.open(image_location) pixels = image.load() except: print "ERROR: Image has failed to load." # setup spaces for space in space_boxes: space_x = space[2] space_y = space[3] space_w = abs(space[4] - space[2]) space_h = abs(space[5] - space[3]) #space_x, space_y, space_w, space_h = __get_area_values(space) print "Space dims:", "x", space_x, "y", space_y, "w", space_w, "h", space_h space_averages.append( imageread.get_area_average(pixels, space_x, space_y, space_w, space_h)) # setup control for control in control_boxes: control_x = control[2] control_y = control[3] control_w = abs(control[4] - control[2]) control_h = abs(control[5] - control[3]) #control_x, control_y, control_w, control_h = __get_area_values(control) print "Control dims:", "x", control_x, "y", control_y, "w", control_w, "h", control_h control_averages.append( imageread.get_area_average(pixels, control_x, control_y, control_w, control_h)) print "\n\n" # compare control points to spaces for i, space in zip(space_boxes, space_averages): num_controls = 0 for control in control_averages: if imageread.compare_area(space, control): num_controls += 1 # Determine if occupied is_occupied = False if num_controls >= 2: is_occupied = True print "INFO: Space", i[0], "is", ("occupied" if is_occupied else "vacant"), "\n" if last_status[i[0]] != is_occupied: print "INFO: Detected change in space", i[0] print "INFO: Space", i[0], "has been the", ( "occupied" if is_occupied else "vacant"), "for", last_ticks[i[0]], "tick(s).\n" if last_ticks[i[0]] < 3: last_ticks[i[0]] += 1 else: last_status[i[0]] = is_occupied last_ticks[i[0]] = 1 print "INFO: Space", i[ 0], "has changed status, sending update to server...\n" num = 1 if is_occupied else 0 print senddata.send_update(i[0], num), "\n" else: last_ticks[i[0]] = 1 # old version """if num_controls >= 2: # last_status print "INFO: Space", i[0], "occupied!" print senddata.send_update(i[0], 1), "\n" else: print "INFO: Space", i[0], "vacant!" print senddata.send_update(i[0], 0), "\n" """ print "INFO: Sleeping for 5s" imageread.time.sleep(5)
def run(): """ Run the main PSVD program. This function periodically captures a new image and then tests for changes in the parking space reference areas compared to the control points as set during the setup procedure (./psvd_setup.py). When a change has been detected for 3 ticks the server (to which the pi is registered) is updated to accordingly show whether the appropriate parking spaces are filled or empty. This function is run mainly as an infinite loop until the application is destroyed. """ if s.IS_VERBOSE: print "INFO: run() called. " # --- Pre-loop Setup ------------------------------------------------------ # variables global camera # use global pi camera object! global occupancy global app image_location = "./images/psvd.jpeg" # image save location loop_delay = s.PICTURE_DELAY # duration between each loop in seconds # load data sets and count the number of spaces and control boxes space_boxes, control_boxes = __setup_box_data() num_spaces = len(space_boxes) num_controls = len(control_boxes) if s.IS_VERBOSE: print "INFO: #Spaces:", num_spaces, "\t#CPs:", num_controls # assert that the correct number of spaces and CPs are present in the data assert num_spaces > 0 assert num_controls == 3 # set initial values for status and ticks last_status = [None for i in range(10)] last_ticks = [3 for i in range(10)] while True: # --- Space and CP Average Calculation Phase --------------------------- # clear lists containing average colour values for spaces space_averages = [] control_averages = [] # capture new image & save to specified location camera.capture(image_location) print "INFO: New image saved to:", image_location try: # load image for processing image = imageread.Image.open(image_location) pixels = image.load() except: print "ERROR: The image has failed to load. Check camera setup. " sys.exit(1) # setup space dimensions and averages, and if verbose, print to terminal for space in space_boxes: space_x = space[2] space_y = space[3] space_w = abs(space[4] - space[2]) space_h = abs(space[5] - space[3]) if s.IS_VERBOSE: print "INFO: Space", space[0], "dimensions:" print " x:", space_x, "y:", space_y, "w:", space_w, "h:", space_h # append space average pixel to list of averages space_average = imageread.get_area_average(pixels, space_x, space_y, space_w, space_h) space_averages.append(space_average) if s.IS_VERBOSE: print "" # line break # setup CP dimensions and averages and, if verbose, print to terminal for control in control_boxes: control_x = control[2] control_y = control[3] control_w = abs(control[4] - control[2]) control_h = abs(control[5] - control[3]) if s.IS_VERBOSE: print "INFO: CP", control[0], "dimensions:" print " x:", control_x, "y:", control_y, "w:", control_w, "h:", control_h # append control average pixel to list of averages control_average = imageread.get_area_average( pixels, control_x, control_y, control_w, control_h) control_averages.append(control_average) # --- Average Comparisons and Data Upload Phase ------------------------ # average pixel values now calculated for all Control Points and Parking # Spaces. Now move on to comparison and upload phase. if s.IS_VERBOSE: print "\n\n" # doubleline break # compare control points averages to parking spaces averages for i, space in zip(space_boxes, space_averages): # number of control points that conflict with parking space reading num_controls = 0 print "INFO: Checking for differences...\n ", # for each control point (3 in total) compare each parking space for control in control_averages: # make comparison if imageread.compare_area(space, control): num_controls += 1 print "Y", else: print "N", # determine if parking space is occupied. If at least two CPs agree # that the space is occupied, set the space to occupied. is_occupied = False if num_controls >= 2: is_occupied = True if s.IS_VERBOSE and is_occupied: print "=> Space", i[0], "is occupied.\n" elif s.IS_VERBOSE and not is_occupied: print "=> Space", i[0], "is empty.\n" # update the server with most recent space values after 3 ticks if last_status[i[0]] != is_occupied: print " Detected change in space", i[0] print " Space", i[0], "has been", ( "occupied" if is_occupied else "vacant"), "for", last_ticks[i[0]], "tick(s).\n" if last_ticks[i[0]] < 3: last_ticks[i[0]] += 1 else: last_status[i[0]] = is_occupied last_ticks[i[0]] = 1 print " Space", i[ 0], "has changed status, sending update to server...\n" num = 1 if is_occupied else 0 occupancy = last_status sendoutput = senddata.send_update(i[0], num) if "success" in sendoutput.keys(): print " Success:", sendoutput["success"] elif "error" in sendoutput.keys(): print " Error:", sendoutput["error"] print '' else: last_ticks[i[0]] = 1 app.updateText() if s.IS_VERBOSE: print "INFO: Sleep for", loop_delay, "seconds..." imageread.time.sleep(loop_delay)
def run(): """ Run the main PiPark program. This function periodically captures a new image and then tests for changes in the parking space reference areas compared to the control points as set during the setup procedure (./pipark_setup.py). When a change has been detected for 3 ticks the server (to which the pi is registered) is updated to accordingly show whether the appropriate parking spaces are filled or empty. This function is run mainly as an infinite loop until the application is destroyed. """ if s.IS_VERBOSE: print "INFO: run() called. " # --- Pre-loop Setup ------------------------------------------------------ # variables global camera # use global pi camera object! global occupancy global app image_location = "./images/pipark.jpeg" # image save location loop_delay = s.PICTURE_DELAY # duration between each loop in seconds # load data sets and count the number of spaces and control boxes space_boxes, control_boxes = __setup_box_data() num_spaces = len(space_boxes) num_controls = len(control_boxes) if s.IS_VERBOSE: print "INFO: #Spaces:", num_spaces, "\t#CPs:", num_controls # assert that the correct number of spaces and CPs are present in the data assert num_spaces > 0 assert num_controls == 3 # set initial values for status and ticks last_status = [None for i in range(10)] last_ticks = [3 for i in range(10)] while True: # --- Space and CP Average Calculation Phase --------------------------- # clear lists containing average colour values for spaces space_averages = [] control_averages = [] # capture new image & save to specified location camera.capture(image_location) print "INFO: New image saved to:", image_location try: # load image for processing image = imageread.Image.open(image_location) pixels = image.load() except: print "ERROR: The image has failed to load. Check camera setup. " sys.exit(1) # setup space dimensions and averages, and if verbose, print to terminal for space in space_boxes: space_x = space[2] space_y = space[3] space_w = abs(space[4] - space[2]) space_h = abs(space[5] - space[3]) if s.IS_VERBOSE: print "INFO: Space", space[0], "dimensions:" print " x:", space_x, "y:", space_y, "w:", space_w, "h:", space_h # append space average pixel to list of averages space_average = imageread.get_area_average( pixels, space_x, space_y, space_w, space_h ) space_averages.append(space_average) if s.IS_VERBOSE: print "" # line break # setup CP dimensions and averages and, if verbose, print to terminal for control in control_boxes: control_x = control[2] control_y = control[3] control_w = abs(control[4] - control[2]) control_h = abs(control[5] - control[3]) if s.IS_VERBOSE: print "INFO: CP", control[0], "dimensions:" print " x:", control_x, "y:", control_y, "w:", control_w, "h:", control_h # append control average pixel to list of averages control_average = imageread.get_area_average( pixels, control_x, control_y, control_w, control_h ) control_averages.append(control_average) # --- Average Comparisons and Data Upload Phase ------------------------ # average pixel values now calculated for all Control Points and Parking # Spaces. Now move on to comparison and upload phase. if s.IS_VERBOSE: print "\n\n" # doubleline break # compare control points averages to parking spaces averages for i, space in zip(space_boxes, space_averages): # number of control points that conflict with parking space reading num_controls = 0 print "INFO: Checking for differences...\n ", # for each control point (3 in total) compare each parking space for control in control_averages: # make comparison if imageread.compare_area(space, control): num_controls += 1 print "Y", else: print "N", # determine if parking space is occupied. If at least two CPs agree # that the space is occupied, set the space to occupied. is_occupied = False if num_controls >= 2: is_occupied = True if s.IS_VERBOSE and is_occupied: print "=> Space", i[0], "is filled.\n" elif s.IS_VERBOSE and not is_occupied: print "=> Space", i[0], "is empty.\n" # update the server with most recent space values after 3 ticks if last_status[i[0]] != is_occupied: print " Detected change in space", i[0] print " Space", i[0], "has been", ("occupied" if is_occupied else "vacant"), "for", last_ticks[i[0]], "tick(s).\n" if last_ticks[i[0]] < 3: last_ticks[i[0]] += 1 else: last_status[i[0]] = is_occupied last_ticks[i[0]] = 1 print " Space", i[0], "has changed status, sending update to server...\n" num = 1 if is_occupied else 0 occupancy = last_status sendoutput = senddata.send_update(i[0], num) if "success" in sendoutput.keys(): print " Success:", sendoutput["success"] elif "error" in sendoutput.keys(): print " Error:", sendoutput["error"] print '' else: last_ticks[i[0]] = 1 app.updateText() if s.IS_VERBOSE: print "INFO: Sleep for", loop_delay, "seconds... Zzz." imageread.time.sleep(loop_delay)
def run(): global camera global occupancy image_location = "/home/pi/BOX/ColorTest" loop_delay = s.PICTURE_DELAY space_boxes, control_boxes = __setup_box_data() num_spaces = len(space_boxes) num_controls = len(control_boxes) assert num_spaces > 0 assert num_controls == 5 #Settings defaults to be used in the first run of the day/test set previousVals = [] checkAvgs = [] prevAvailability = [] for i in range(num_spaces): previousVals.append([]) checkAvgs.append(false) prevAvailability.append(true) for j in range(0, 5): previousVals[i].append(-1) camera = picamera.PiCamera() camera.resolution = (2592, 1944) print "Initialized Camera" for locNum in range(s.MAX_LOOPS): locationNum = str(locNum) space_averages = [] control_averages = [] startTime = time.time() try: loc = image_location + locationNum + ".jpg" camera.capture(loc) print "Camera Just Captured and Image! Analyzing now.." print "" image = imageread.Image.open(loc) #image = image.transpose(imageread.Image.FLIP_TOP_BOTTOM) #image = image.transpose(imageread.Image.FLIP_LEFT_RIGHT) if s.NUM_COLORS == 1: image = image.convert('L') #image = image.convert('1') pixels = image.load() except: print "Error while loading image" sys.exit(1) ################################################################################# spaceIndex = 0 for space in space_boxes: checkAvgs[spaceIndex] = checkCriticalAreas( critPoints, pixels, previousVals[spaceIndex]) if checkAvgs[spaceIndex]: space_average = imageread.get_area_average( pixels, space[2], space[3], abs(space[2] - space[4]), abs(space[3] - space[5])) space_averages.append(space_average) spaceIndex += 1 ################################################################################# controlIndex = 0 for control in control_boxes: if checkAvgs[controlIndex]: control_average = imageread.get_area_average( pixels, control[2], control[3], abs(control[2] - control[4]), abs(control[3] - control[5])) control_averages.append(control_average) controlIndex += 1 ################################################################################# counter = 1 numSpots = 24 numRows = 4 checkIndex = 0 for i, space in zip(space_boxes, space_averages): couter = counter + 1 num_controls = 0 is_occupied = False for control in control_averages: if imageread.compare_area(space, control): num_controls += 1 if num_controls >= 3: is_occupied = True if is_occupied: print "x " else: print "o " if counter % (numSpots / numRows) == 0: print "\n" if previousVals[0][0] != -1: prevAvailability[checkIndex] = is_occupied checkIndex += 1 for i in range(len(previousVals)): previousVals[i] = space_averages[i] print "It took ", (time.time() - startTime), " time to complete this run." print "\n\n" imageread.time.sleep(loop_delay)
def run(): global camera global occupancy global app image_location = "./images/pipark.jpeg" loop_delay = s.PICTURE_DELAY space_boxes, control_boxes = __setup_box_data() num_spaces = len(space_boxes) num_controls = len(control_boxes) assert num_spaces > 0 assert num_controls == 3 last_status = [None for i in range(10)] last_ticks = [3 for i in range(10)] while True: space_averages = [] control_averages = [] camera.capture(image_location) try: image = imageread.Image.open(image_location) pixels = image.load() except: print "ERROR: Check camera" sys.exit(1) for space in space_boxes: space_x = space[2] space_y = space[3] space_w = abs(space[4] - space[2]) space_h = abs(space[5] - space[3]) space_average = imageread.get_area_average( pixels, space_x, space_y, space_w, space_h ) space_averages.append(space_average) for control in control_boxes: control_x = control[2] control_y = control[3] control_w = abs(control[4] - control[2]) control_h = abs(control[5] - control[3]) control_average = imageread.get_area_average( pixels, control_x, control_y, control_w, control_h ) control_averages.append(control_average) if s.IS_VERBOSE: print "\n\n" for i, space in zip(space_boxes, space_averages): num_controls = 0 print "", for control in control_averages: if imageread.compare_area(space, control): num_controls += 1 print "", else: print "", is_occupied = False if num_controls >= 2: is_occupied = True if s.IS_VERBOSE and is_occupied: print "Number", i[0], "is filled.\n" elif s.IS_VERBOSE and not is_occupied: print "Number", i[0], "is empty.\n" if last_status[i[0]] != is_occupied: if last_ticks[i[0]] < 2: last_ticks[i[0]] += 1 else: last_status[i[0]] = is_occupied last_ticks[i[0]] = 1 num = 1 if is_occupied else 0 occupancy = last_status else: last_ticks[i[0]] = 1 app.updateText() imageread.time.sleep(loop_delay)
def main(): """ Run main program loop. Detect changes to parking spaces and update appropriate availabity of the spaces to the server. """ # setup camera and image save location camera = imageread.setup_camera() camera.start_preview() image_location = "./images/pipark.jpeg" # load data sets and count num spaces on control boxes space_boxes, control_boxes = __setup_box_data() num_spaces = len(space_boxes) num_controls = len(control_boxes) print "Number of spaces:", num_spaces, "Number of Control boxes:", num_controls assert num_spaces > 0 assert num_controls == 3 last_status = [None for i in range(10)] last_ticks = [3 for i in range(10)] # run centralised program loop while True: space_averages = [] control_averages = [] # take new picture, save to specified location camera.capture(image_location) print "INFO: New picture taken,", image_location # load image try: image = imageread.Image.open(image_location) pixels = image.load() except: print "ERROR: Image has failed to load." # setup spaces for space in space_boxes: space_x = space[2] space_y = space[3] space_w = abs(space[4] - space[2]) space_h = abs(space[5] - space[3]) # space_x, space_y, space_w, space_h = __get_area_values(space) print "Space dims:", "x", space_x, "y", space_y, "w", space_w, "h", space_h space_averages.append(imageread.get_area_average(pixels, space_x, space_y, space_w, space_h)) # setup control for control in control_boxes: control_x = control[2] control_y = control[3] control_w = abs(control[4] - control[2]) control_h = abs(control[5] - control[3]) # control_x, control_y, control_w, control_h = __get_area_values(control) print "Control dims:", "x", control_x, "y", control_y, "w", control_w, "h", control_h control_averages.append(imageread.get_area_average(pixels, control_x, control_y, control_w, control_h)) print "\n\n" # compare control points to spaces for i, space in zip(space_boxes, space_averages): num_controls = 0 for control in control_averages: if imageread.compare_area(space, control): num_controls += 1 # Determine if occupied is_occupied = False if num_controls >= 2: is_occupied = True print "INFO: Space", i[0], "is", ("occupied" if is_occupied else "vacant"), "\n" if last_status[i[0]] != is_occupied: print "INFO: Detected change in space", i[0] print "INFO: Space", i[0], "has been the", ("occupied" if is_occupied else "vacant"), "for", last_ticks[ i[0] ], "tick(s).\n" if last_ticks[i[0]] < 3: last_ticks[i[0]] += 1 else: last_status[i[0]] = is_occupied last_ticks[i[0]] = 1 print "INFO: Space", i[0], "has changed status, sending update to server...\n" num = 1 if is_occupied else 0 print senddata.send_update(i[0], num), "\n" else: last_ticks[i[0]] = 1 # old version """if num_controls >= 2: # last_status print "INFO: Space", i[0], "occupied!" print senddata.send_update(i[0], 1), "\n" else: print "INFO: Space", i[0], "vacant!" print senddata.send_update(i[0], 0), "\n" """ print "INFO: Sleeping for 5s" imageread.time.sleep(5)
def run(): global camera global occupancy image_location = "/home/pi/BOX/ColorTest" loop_delay = s.PICTURE_DELAY space_boxes, control_boxes = __setup_box_data() num_spaces = len(space_boxes) num_controls = len(control_boxes) assert num_spaces > 0 assert num_controls == 5 last_status = [None for i in range(10)] last_ticks = [3 for i in range(10)] camera = picamera.PiCamera() camera.resolution = (2592, 1944) print "Initialized Camera" for locNum in range(s.MAX_LOOPS): locationNum = str(locNum) space_averages = [] control_averages = [] startTime = time.time() #6/26 UPDATE FOR DATA COLLECTION #compareIterator = 0 #compareNum = 0 try: loc = image_location + locationNum + ".jpg" camera.capture(loc) print "Camera Just Captured and Image! Analyzing now.." print "" image = imageread.Image.open(loc) image = image.transpose(imageread.Image.FLIP_TOP_BOTTOM) image = image.transpose(imageread.Image.FLIP_LEFT_RIGHT) if s.NUM_COLORS == 1: image = image.convert('L') #image = image.convert('1') pixels = image.load() except: print "Error while loading image" sys.exit(1) ################################################################################# for space in space_boxes: space_average = imageread.get_area_average( pixels, space[2], space[3], abs(space[2] - space[4]), abs(space[3] - space[5]) ) space_averages.append(space_average) ################################################################################# for control in control_boxes: control_average = imageread.get_area_average( pixels, control[2], control[3], abs(control[2] - control[4]), abs(control[3] - control[5]) ) control_averages.append(control_average) ################################################################################# counter = 1 numSpots = 24 numRows = 4 for i, space in zip(space_boxes, space_averages): # number of control points that conflict with readings num_controls = 0 is_occupied = False for control in control_averages: if imageread.compare_area(space, control): num_controls += 1 if num_controls >= 3: is_occupied = True if s.IS_VERBOSE and is_occupied: print "x ", elif s.IS_VERBOSE and not is_occupied: print "o ", couter = counter + 1 if counter%(numSpots/numRows) == 0: print "\n" counter += 1 #6/26 UPDATE FOR DATA COLLECTION #Set comparison number to compare results with actual information #compareNum = counter + (numSpots*locNum) #if is_occupied == #{actual boolean value of spot}: # #{append "correct" value for this spot"} #if not, append incorrect value print "It took ", (time.time() - startTime), " time to complete this run." print "\n\n" imageread.time.sleep(loop_delay)