diff --git a/85_Cipher/directions.py b/85_Cipher/directions.py new file mode 100644 index 00000000..71d0213a --- /dev/null +++ b/85_Cipher/directions.py @@ -0,0 +1,45 @@ +import googlemaps + +# Initialize the Google Maps client +API_KEY = 'xyz' # API key +gmaps = googlemaps.Client(key=API_KEY) + +def get_directions(start_place, destination_place): + try: + start_coords = gmaps.geocode(start_place) + destination_coords = gmaps.geocode(destination_place) + + if start_coords and destination_coords: + start_latlng = start_coords[0]['geometry']['location'] + dest_latlng = destination_coords[0]['geometry']['location'] + + directions = gmaps.directions( + origin=start_latlng, + destination=dest_latlng, + mode="driving" + ) + steps = directions[0]['legs'][0]['steps'] + waypoints = [] + + for step in steps: + lat = step['end_location']['lat'] + lng = step['end_location']['lng'] + waypoints.append((lat, lng)) + + return waypoints + else: + print("Geocoding failed for one of the locations.") + except googlemaps.exceptions.ApiError as e: + print(f"API error: {e}") + except Exception as e: + print(f"An error occurred: {e}") + + +start_place = "Pocket B2, Mayur Vihar Phase 3, Delhi 110096" +destination_place = "SFS flats, Mayur Vihar Phase 3, Delhi 110096" + +waypoints = get_directions(start_place, destination_place) + +print("Waypoints along the route:") +for wp in waypoints: + print(f"Lat: {wp[0]}, Lng: {wp[1]}") diff --git a/85_Cipher/obstacle detection(mog).py b/85_Cipher/obstacle detection(mog).py new file mode 100644 index 00000000..14b6d72c --- /dev/null +++ b/85_Cipher/obstacle detection(mog).py @@ -0,0 +1,83 @@ +import cv2 + + +def detect_obstacles(frame, bg_subtractor, area_threshold, debug=False): + # background subtractor + fg_mask = bg_subtractor.apply(frame) + + + kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5)) + + fg_mask = cv2.morphologyEx(fg_mask, cv2.MORPH_OPEN, kernel) + fg_mask = cv2.morphologyEx(fg_mask, cv2.MORPH_DILATE, kernel) + + # Find contours + contours, _ = cv2.findContours(fg_mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) + + obstacles = [] + for contour in contours: + area = cv2.contourArea(contour) + if area > area_threshold: + x, y, w, h = cv2.boundingRect(contour) + obstacles.append((x, y, w, h)) + + if debug: + return obstacles, fg_mask + return obstacles + + +def nothing(x): + """Eat 5 star, do nothing.""" + pass + + +def main(): + # Initialize webcam + cap = cv2.VideoCapture(0) + if not cap.isOpened(): + print("Error: Could not open webcam.") + return + + # background subtractor (MOG2) + bg_subtractor = cv2.createBackgroundSubtractorMOG2(history=500, varThreshold=32, detectShadows=True) + + cv2.namedWindow('Settings') + cv2.createTrackbar('Area Threshold', 'Settings', 15000, 20000, nothing) + + while True: + ret, frame = cap.read() + if not ret: + print("Failed to grab frame") + break + + + area_thresh = cv2.getTrackbarPos('Area Threshold', 'Settings') + + # Detect obstacles + obstacles, fg_mask = detect_obstacles(frame, bg_subtractor, area_threshold=area_thresh, debug=True) + + # bounding boxes + if obstacles: + cv2.putText(frame, "Obstacle Detected!", (50, 50), + cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2) + for (x, y, w, h) in obstacles: + cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 0, 255), 2) + else: + cv2.putText(frame, "No Obstacle", (50, 50), + cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2) + + # Display the live feed + cv2.imshow("Webcam Feed", frame) + cv2.imshow("Foreground Mask", fg_mask) + + # Exit the loop when 'q' is pressed + if cv2.waitKey(1) & 0xFF == ord('q'): + break + + # Cleanup + cap.release() + cv2.destroyAllWindows() + + +if __name__ == '__main__': + main() diff --git a/85_Cipher/obstacle_detection.py b/85_Cipher/obstacle_detection.py new file mode 100644 index 00000000..e5b0383c --- /dev/null +++ b/85_Cipher/obstacle_detection.py @@ -0,0 +1,80 @@ +import cv2 + + +def detect_obstacles(frame, threshold_value=200, area_threshold=8000, debug=False): + + # grayscale and Gaussian blur + gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) + blurred_frame = cv2.GaussianBlur(gray_frame, (5, 5), 0) + + # binary image + _, thresh_img = cv2.threshold(blurred_frame, threshold_value, 255, cv2.THRESH_BINARY) + + # Find contours + contours, _ = cv2.findContours(thresh_img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) + + obstacles = [] + for contour in contours: + area = cv2.contourArea(contour) + if area > area_threshold: + # Get bounding rectangle for each obstacle + x, y, w, h = cv2.boundingRect(contour) + obstacles.append((x, y, w, h)) + + if debug: + return obstacles, thresh_img + return obstacles + + +def nothing(x): + """Eat 5 star, Do nothing.""" + pass + + +def main(): + # Initialize webcam + cap = cv2.VideoCapture(0) + if not cap.isOpened(): + print("Error: Could not open webcam.") + return + + cv2.namedWindow('Settings') + cv2.createTrackbar('Threshold', 'Settings', 200, 255, nothing) + cv2.createTrackbar('Area Threshold', 'Settings', 8000, 20000, nothing) + + while True: + ret, frame = cap.read() + if not ret: + print("Failed to grab frame") + break + + thresh_value = cv2.getTrackbarPos('Threshold', 'Settings') + area_thresh = cv2.getTrackbarPos('Area Threshold', 'Settings') + + obstacles, thresh_img = detect_obstacles(frame, threshold_value=thresh_value, + area_threshold=area_thresh, debug=True) + + if obstacles: + cv2.putText(frame, "Obstacle Detected!", (50, 50), + cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2) + for (x, y, w, h) in obstacles: + cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 0, 255), 2) + else: + cv2.putText(frame, "No Obstacle", (50, 50), + cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2) + + # Display the live feed + cv2.imshow("Webcam Feed", frame) + cv2.imshow("Threshold Image", thresh_img) + + # Exit the loop when 'q' is pressed + if cv2.waitKey(1) & 0xFF == ord('q'): + break + + # Cleanup + cap.release() + cv2.destroyAllWindows() + + +if __name__ == '__main__': + main()