Spaces:
Sleeping
Sleeping
Update app.py
Browse filesYOLO5V with OpenPilot Trajectory Prediction
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960613 unmodified lines6566676869from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,toolimport datetimeimport requestsimport pytzimport yamlfrom tools.final_answer import FinalAnswerToolfrom Gradio_UI import GradioUI# Below is an example of a tool that does nothing. Amaze us with your creativity !@tooldef my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type#Keep this format for the description / args / args description but feel free to modify the tool"""A tool that does nothing yetArgs:arg1: the first argumentarg2: the second argument"""return "What magic will you build ?"@tooldef get_current_time_in_timezone(timezone: str) -> str:"""A tool that fetches the current local time in a specified timezone.Args:timezone: A string representing a valid timezone (e.g., 'America/New_York')."""try:# Create timezone objecttz = pytz.timezone(timezone)# Get current time in that timezonelocal_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")return f"The current local time in {timezone} is: {local_time}"except Exception as e:return f"Error fetching time for timezone '{timezone}': {str(e)}"final_answer = FinalAnswerTool()# If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder:# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'model = HfApiModel(max_tokens=2096,temperature=0.5,model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloadedcustom_role_conversions=None,)# Import tool from Hubimage_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)with open("prompts.yaml", 'r') as stream:prompt_templates = yaml.safe_load(stream)agent = CodeAgent(model=model,tools=[final_answer], ## add your tools here (don't remove final answer)max_steps=6,verbosity_level=1,grammar=None,3 unmodified linesprompt_templates=prompt_templates)GradioUI(agent).launch()1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541553 unmodified lines159160161162from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, toolimport datetimeimport requestsimport pytzimport yamlfrom tools.final_answer import FinalAnswerToolfrom ultralytics import YOLO # YOLOv5 modelimport cv2import numpy as npfrom Gradio_UI import GradioUI@tooldef get_yolov5_coco_detections(image_path: str) -> dict:"""Detects objects using YOLOv5 on the COCO dataset and provides structured outputs."""model = YOLO("yolov5s.pt")image = cv2.imread(image_path)results = model(image)detections = []if results:for r in results.pred[0]:x1, y1, x2, y2, conf, cls = r.tolist()class_name = model.names[int(cls)]detections.append({"object": class_name, "confidence": conf, "bbox": (x1, y1, x2, y2)})return {"detected_objects": detections} if detections else {"detected_objects": []}@tooldef detect_road_lanes(image_path: str) -> dict:"""Detects road lanes using a YOLOv5 model trained for lane detection."""model = YOLO("yolov5-lane.pt")image = cv2.imread(image_path)results = model(image)lane_detections = []if results:for r in results.pred[0]:x1, y1, x2, y2, conf, cls = r.tolist()lane_detections.append({"lane": f"Lane {cls}", "confidence": conf, "bbox": (x1, y1, x2, y2)})return {"detected_lanes": lane_detections} if lane_detections else {"detected_lanes": []}@tooldef driving_situation_analyzer(image_path: str) -> dict:"""Analyzes road conditions by integrating object detections and lane information."""objects_info = get_yolov5_coco_detections(image_path)lanes_info = detect_road_lanes(image_path)detected_objects = objects_info.get("detected_objects", [])detected_lanes = lanes_info.get("detected_lanes", [])situation = []if any(obj["object"] in ["car", "truck", "bus"] for obj in detected_objects):situation.append("Traffic detected ahead, maintain safe distance.")if any(obj["object"] == "pedestrian" for obj in detected_objects):situation.append("Pedestrian detected, be prepared to stop.")if any(obj["object"] == "traffic light" for obj in detected_objects):situation.append("Traffic light detected, slow down if red.")if not detected_lanes:situation.append("Lane markings not detected, potential risk of veering.")elif len(detected_lanes) == 1:situation.append("Single lane detected, ensure proper lane following.")elif len(detected_lanes) >= 2:situation.append("Multiple lanes detected, stay within lane boundaries.")return {"situation_summary": " | ".join(situation) if situation else "Road situation unclear, proceed with caution.","detected_objects": detected_objects,"detected_lanes": detected_lanes}@tooldef predict_trajectory(image_path: str) -> dict:"""Predicts vehicle trajectory based on the driving situation analysis.Uses OpenPilot-style motion prediction based on detected lanes, objects, and road conditions."""analysis = driving_situation_analyzer(image_path)detected_objects = analysis["detected_objects"]detected_lanes = analysis["detected_lanes"]summary = analysis["situation_summary"]trajectory = []# Define simple trajectory logicif "Traffic detected" in summary:trajectory.append("Reduce speed, maintain a safe distance.")if "Pedestrian detected" in summary:trajectory.append("Prepare for sudden braking or yielding.")if "Traffic light detected" in summary:trajectory.append("Adjust speed based on light status.")if "Lane markings not detected" in summary:trajectory.append("Risk of lane departure, drive cautiously.")if len(detected_lanes) >= 2:trajectory.append("Stay centered in the lane, adjust for merging vehicles.")# Generate simple trajectory points (Mocked example)future_positions = []x, y = 0, 0for t in range(10): # Predict 10 future stepsx += np.random.uniform(-0.5, 0.5) # Small lateral deviationy += 1 # Move forwardfuture_positions.append((x, y))return {"trajectory_recommendation": " | ".join(trajectory) if trajectory else "Maintain current path.","future_positions": future_positions}@tooldef get_current_time_in_timezone(timezone: str) -> str:"""Fetches the current local time in a specified timezone."""try:tz = pytz.timezone(timezone)local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")return f"The current local time in {timezone} is: {local_time}"except Exception as e:return f"Error fetching time for timezone '{timezone}': {str(e)}"final_answer = FinalAnswerTool()model = HfApiModel(max_tokens=2096,temperature=0.5,model_id='Qwen/Qwen2.5-Coder-32B-Instruct',custom_role_conversions=None,)image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)with open("prompts.yaml", 'r') as stream:prompt_templates = yaml.safe_load(stream)agent = CodeAgent(model=model,tools=[final_answer,get_yolov5_coco_detections,detect_road_lanes,driving_situation_analyzer,predict_trajectory],max_steps=6,verbosity_level=1,grammar=None,3 unmodified linesprompt_templates=prompt_templates)GradioUI(agent).launch()
app.py+122-29