def hanlde_endpoint_scan(self, image, json) -> int: filename, full_path = compute_file_name() # Save the image using the flask file image.save(full_path) scan_request = ScanRequest.from_request(json, filename) return self.handle_scan(scan_request, filename)
def create_scan(): img: np.ndarray = cv.imread("raw.jpg") depth_map = np.random.rand(img.shape[0], img.shape[1]) scan_req = ScanRequest(img, depth_map, 2, 1) from tray_system.data_pusher import DataPusher dp = DataPusher() dp.push_scan(scan_req)
def scan(self): if self.last_rfid is None: print("Tried to scan without RFID") return colour_img, depth_img = self.realsense_capturer.capture() print(colour_img.shape) print(depth_img.shape) menu_item_id = 1 # Get this from RFID user_id = 1 scan_request = ScanRequest(colour_img, depth_img, menu_item_id, user_id) if COMPUTE_LOCAL: return self.scan_handler.handle_local_scan(scan_request) return self.data_pusher.push_scan(scan_request)
def push_scan(self, scan_request: ScanRequest): _, img_encoded = cv.imencode('.jpg', scan_request.image) payload = { 'json': (None, scan_request.get_json(), 'application/json'), 'image': ("ignored.jpg", img_encoded), } resp = requests.post(Endpoint.SCAN.get(), files=payload) if resp.status_code == requests.codes.ok: print("Successfully pushed scan to server") else: print("Unable to push scan to server") # TODO: Return scan id return resp.data
# max_depth_in_circle = segmented_circle.get_max_value_in_circle(depth_map) # print("Max depth in circle was %d" % max_depth_in_circle) # depth_map = max_depth_in_circle - depth_map for segment in segmented_circle.segments: ingredient = self.ingredient_detector.label( segment.get_segment_of(image)) if ingredient is None: continue mass = calculate_mass(depth_map, segment, mm_per_pixel) print(mass) detection: Detection = Detection(segment.x1, segment.y1, mass) detected_ingredients[ingredient].append(detection) results = [ DetectedIngredient(scan_id, k.id, v) for (k, v) in detected_ingredients.items() ] print("Found {} detected ingredients: {}".format( len(results), [r.ingredient_id for r in results])) return results if __name__ == "__main__": img: np.ndarray = cv.imread("../../test.jpg") depth_map = 500 * np.random.rand(img.shape[0], img.shape[1]) scan_req = ScanRequest(img, depth_map, 1, 1) from tray_system.data_pusher import DataPusher dp = DataPusher() dp.push_scan(scan_req) # detector.run_detection(scan_req)
def run_live(ingredient_detector): data_pusher = DataPusher() pipeline = rs.pipeline() config = rs.config() config.enable_stream(rs.stream.depth, REALSENSE_WIDTH, REALSENSE_HEIGHT, rs.format.z16, 30) config.enable_stream(rs.stream.color, REALSENSE_WIDTH, REALSENSE_HEIGHT, rs.format.bgr8, 30) align_to = rs.stream.color align = rs.align(align_to) # Start streaming pipeline.start(config) colorizer = rs.colorizer() hole_filler = rs.hole_filling_filter() # Skip 5 first frames to give the Auto-Exposure time to adjust for x in range(5): pipeline.wait_for_frames() while True: frames = pipeline.wait_for_frames(timeout_ms=5000) aligned_frames = align.process(frames) color = aligned_frames.get_color_frame() depth = aligned_frames.get_depth_frame() # depth = hole_filler.process(depth) # depth2 = depth.get_distance(100, 100) # print(depth2) if not depth or not color: continue depth_frame_data = np.asanyarray(depth.get_data()) color_frame_data = np.asanyarray(color.get_data()) depth_image = np.asanyarray(colorizer.colorize(depth).get_data(), dtype=np.uint8) # depth_image = np.asanyarray(depth.get_data(), dtype=np.uint8) color_image = np.asanyarray(color.get_data(), dtype=np.uint8) color_image_with_overlay = np.copy(color_image) # cv.imwrite("rgb.jpg", color_image) # cv.imwrite("depth.jpg", depth_image) process_colour_image(color_image_with_overlay, ingredient_detector) circle_detector.draw_segmented_circle(color_image_with_overlay) if cv.waitKey(1) & 0xFF == ord(' '): # np.save("colour.txt", color_image) # np.save("depth.txt", np.asanyarray(depth.get_data(), dtype=np.uint8)) # r = color_image[:, :, 0] # g = color_image[:, :, 1] # b = color_image[:, :, 2] # np.savetxt("colour-r.txt", r) # np.savetxt("colour-g.txt", g) # np.savetxt("colour-b.txt", b) # np.savetxt("depth.txt", np.asanyarray(depth.get_data(), dtype=np.uint8)) scan_request = ScanRequest(color_frame_data, depth_frame_data, 1, 1) data_pusher.push_scan(scan_request) check_for_key() cv.imshow("Depth", depth_image) cv.imshow("RGB", color_image_with_overlay) sleep(0.2)