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 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 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)