Tuesday, July 22, 2025

 Agent to detect vehicles in aerial drone images: 

#!/usr/bin/python  

# azure-ai-agents==1.0.0  

# azure-ai-projects==1.0.0b11  

# azure-ai-vision-imageanalysis==1.0.0  

# azure-common==1.1.28  

# azure-core==1.34.0  

# azure-identity==1.22.0  

# azure-search-documents==11.6.0b12  

# azure-storage-blob==12.25.1  

# azure_ai_services==0.1.0  

from dotenv import load_dotenv  

from azure.identity import DefaultAzureCredential, get_bearer_token_provider  

from azure.ai.agents import AgentsClient  

from azure.core.credentials import AzureKeyCredential  

from azure.ai.projects import AIProjectClient  

from typing import Any, Callable, Set, Dict, List, Optional 

import os, time, sys 

import torch 

from azure.ai.agents import AgentsClient 

from azure.ai.agents.models import ( 

    FunctionTool, 

    ListSortOrder, 

    RequiredFunctionToolCall, 

    SubmitToolOutputsAction, 

    ToolOutput, 

) 

from user_functions import fetch_weather, user_functions 

sys.path.insert(0, os.path.abspath(".")) 

load_dotenv(override=True)  

project_endpoint = os.environ["AZURE_PROJECT_ENDPOINT"]  

project_api_key = os.environ["AZURE_PROJECT_API_KEY"]  

agent_model = os.getenv("AZURE_AGENT_MODEL", "gpt-4o-mini")  

agent_name = os.getenv("AZURE_VEHICLE_COUNT_AGENT_NAME", "vehicle-agent-in-a-team") 

api_version = "2025-05-01-Preview"  

agent_max_output_tokens=10000  

object_uri = os.getenv("AZURE_RED_CAR_2_SAS_URL").strip('"') 

scene_uri = os.getenv("AZURE_QUERY_SAS_URI").strip('"')  

from azure.ai.projects import AIProjectClient  

project_client = AIProjectClient(endpoint=project_endpoint, credential=DefaultAzureCredential())  

agents_client = AgentsClient( 

    endpoint=project_endpoint, 

    credential=DefaultAzureCredential(), 

) 

 

def read_image_from_blob(sas_url): 

    """Reads an image from Azure Blob Storage using its SAS URL.""" 

    response = requests.get(sas_url) 

    if response.status_code == 200: 

        image_array = np.asarray(bytearray(response.content), dtype=np.uint8) 

        image = cv2.imdecode(image_array, cv2.IMREAD_COLOR) 

        return image 

    else: 

        # raise Exception(f"Failed to fetch image. Status code: {response.status_code}") 

        return None 

         

def detect_vehicles(frame): 

    results = model(frame) 

    # Keep only 'car', 'truck', 'bus', 'motorcycle' detections 

    vehicle_labels = ['car', 'truck', 'bus', 'motorcycle'] 

    detections = results.pandas().xyxy[0] 

    vehicles = detections[detections['name'].isin(vehicle_labels)] 

    return vehicles 

 

def get_image_output_url(scene_uri): 

    # Parse the original video URL to get account, container, and path 

    parsed = urlparse(scene_uri) 

    path_parts = parsed.path.split('/') 

    container = path_parts[1] 

    blob_path = '/'.join(path_parts[2:]) 

    # Remove the file name from the blob path 

    blob_dir = '/'.join(blob_path.split('/')[:-1]) 

    if blob_dir == "" or blob_dir == None: 

        blob_dir = "output" 

    # Create image path 

    image_path = f"{blob_dir}/images/vehiclesframe.jpg" 

    # Rebuild the base URL (without SAS token) 

    base_url = f"{parsed.scheme}://{parsed.netloc}/{container}/{image_path}" 

    # Add the SAS token if present 

    sas_token = parsed.query 

    if sas_token: 

        image_url = f"{base_url}?{sas_token}" 

    else: 

        image_url = base_url 

    return image_url 

     

def detect_vehicles_from_uri(scene_uri: Optional[str] = None) -> str: 

    if not scene_uri: 

        return None 

    frame = read_image_from_blob(scene_uri) 

    if not frame: 

        return None 

    vehicles = detect_vehicles(frame) 

    print(vehicles) 

    for _, v in vehicles.iterrows(): 

x1, y1, x2, y2 = map(int, [v['xmin'], v['ymin'], v['xmax'], v['ymax']])  

w, h = x2 - x1, y2 - y1 

        cv2.rectangle(frame, (x, y), (x +w, y + h), (255,0,0), 2) 

    _, buffer = cv2.imencode('.jpg', frame) 

    image_bytes = buffer.tobytes() 

    image_uri = get_image_output_url(scene_uri) 

    image_blob_client = BlobClient.from_blob_url(image_url) 

    image_blob_client.upload_blob(image_bytes, overwrite=True)  

    return image_uri 

     

image_user_functions: Set[Callable[..., Any]] = { 

    detect_vehicles_from_uri 

} 

 

# Initialize function tool with user functions 

functions = FunctionTool(functions=image_user_functions) 

instructions = "You are an assistant that answers the question how many vehicles were found in an image when the image is given by an image URI. You evaluate a function to do this by passing their uri to the function and respond with the count." 

query_text = f"How many vehicles are found in the image given by its image URI {scene_uri}?" 

with agents_client: 

    # Create an agent and run user's request with function calls 

    # agent = agents_client.get_agent(agent_id="asst_qyMFcz1BnU0BS0QUmhxAAyFk") 

    # """ 

    agent = agents_client.create_agent( 

        model=agent_model, 

        name=agent_name, 

        instructions=instructions, 

        tools=functions.definitions, 

        tool_resources=functions.resources, 

        top_p=1 

    ) 

    # """ 

    print(f"Created agent, ID: {agent.id}") 

 

    thread = agents_client.threads.create() 

    print(f"Created thread, ID: {thread.id}") 

 

    message = agents_client.messages.create( 

        thread_id=thread.id, 

        role="user", 

        content=query_text, 

    ) 

    print(f"Created message, ID: {message.id}") 

 

    run = agents_client.runs.create(thread_id=thread.id, agent_id=agent.id) 

    print(f"Created run, ID: {run.id}") 

 

    while run.status in ["queued", "in_progress", "requires_action"]: 

        time.sleep(1) 

        run = agents_client.runs.get(thread_id=thread.id, run_id=run.id) 

 

        if run.status == "requires_action" and isinstance(run.required_action, SubmitToolOutputsAction): 

            tool_calls = run.required_action.submit_tool_outputs.tool_calls 

            if not tool_calls: 

                print("No tool calls provided - cancelling run") 

                agents_client.runs.cancel(thread_id=thread.id, run_id=run.id) 

                break 

 

            tool_outputs = [] 

            for tool_call in tool_calls: 

                if isinstance(tool_call, RequiredFunctionToolCall): 

                    print("Is an instance of RequiredFunctionToolCall") 

                    try: 

                        print(f"Executing tool call: {tool_call}") 

                        output = functions.execute(tool_call) 

                        print(output) 

                        tool_outputs.append( 

                            ToolOutput( 

                                tool_call_id=tool_call.id, 

                                output=output, 

                            ) 

                        ) 

                    except Exception as e: 

                        print(f"Error executing tool_call {tool_call.id}: {e}") 

                else: 

                    print(f"{tool_call} skipped.") 

 

            print(f"Tool outputs: {tool_outputs}") 

            if tool_outputs: 

                agents_client.runs.submit_tool_outputs(thread_id=thread.id, run_id=run.id, tool_outputs=tool_outputs) 

            else: 

                print(f"No tool output.") 

        else: 

            print(f"Waiting: {run}") 

 

        print(f"Current run status: {run.status}") 

 

    print(f"Run completed with status: {run.status} and details {run}") 

 

    # Delete the agent when done 

    agents_client.delete_agent(agent.id) 

    print("Deleted agent") 

 

    # Fetch and log all messages 

    messages = agents_client.messages.list(thread_id=thread.id, order=ListSortOrder.ASCENDING) 

    for msg in messages: 

        if msg.text_messages: 

            last_text = msg.text_messages[-1] 

            print(f"{msg.role}: {last_text.text.value}") 

 

Monday, July 21, 2025

 Micro-batching versus streaming for aerial drone video analytics

Azure AI Vision enables near real-time video analysis by extracting meaningful insights from live video streams. The process involves capturing frames from a video source, selecting which frames to analyze, submitting them to the API, and consuming the returned results. There are three progressively sophisticated approaches to implementing this workflow.

The simplest method uses an infinite loop that grabs a frame, analyzes it, and processes the result. While straightforward, this approach is limited by the latency of cloud-based API calls, which can slow down frame acquisition. To improve performance, the second method introduces parallelism by launching each analysis task asynchronously. This allows frame grabbing to continue uninterrupted but introduces challenges like out-of-order results and potential thread safety issues.

The most robust solution is a producer-consumer system. Here, a producer thread captures frames and queues analysis tasks, while a consumer thread process results sequentially. This design ensures orderly result handling and maximizes frame throughput without blocking operations. This draws inspiration from TCP protocol ring buffer processing and socket programming with overlapped I/O.

To help developers get started, Microsoft provides a sample library called FrameGrabber, which simplifies integration with Azure AI services. It supports event-driven programming, allowing developers to respond to new frames and analysis results efficiently. A sample python application demonstrates how to use the pygrabber for vehicle tracking in aerial drone feeds.

This hybrid approach—combining client-side preprocessing with cloud-based analysis—offers flexibility and scalability for building intelligent video applications by still retaining the bulk of the work on the analytics side instead of the video preprocessing side or with model training, testing and revision cycles. Developers can build interactive experiences on querying aerial drone images using agentic retrieval.

While many purpose-specific aerial drone video sensing applications do require custom models for various purposes, we believe a video sensing analytical platform1 removes much of the overhead and repetitive tasks in home-grown and DIY solutions while moving the complexity from vectorizing to analytics. It also provides an opportunity to stay nimble on alternative or augmentation techniques to image frame grabbing and processing such as with video indexing, thereby avoiding the high cost of repetitive tasks on the video preprocessing side.

Extending the concept of picking what to process from frame selection to video indexing, it becomes clearer that specific analysis can be done with a high degree of accuracy and high performance when videos are micro-batched versus when split into live feed frames by reducing the working set. Only in cases where continuous tracking is required, the latter may be paid for. In most cases, the drone world catalog suffices to be build incrementally from the iterative micro-batches for answering general questions including those relevant to vehicle tracking.

Sample application demonstrating this:

# Step 1: grab frames from live or indexed video

from pygrabber.dshow_graph import FilterGraph

import cv2

# Initialize the frame grabber device (typically, 0 means the first available camera)

graph = FilterGraph()

camera_index = 0

graph.add_video_input_device(camera_index)

# Define a function to process and return frames

def get_camera_frame(image_buffer):

    # Convert the bgr image buffer to an OpenCV image

    frame = image_buffer

    return frame

graph.add_sample_grabber(get_camera_frame)

graph.add_null_render()

graph.prepare_preview_graph()

graph.run()

import torch

# Step 2: detect object

model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True)

def detect_vehicles(frame):

    results = model(frame)

    # Keep only 'car', 'truck', 'bus', 'motorcycle' detections

    vehicle_labels = ['car', 'truck', 'bus', 'motorcycle']

    detections = results.pandas().xyxy[0]

    vehicles = detections[detections['name'].isin(vehicle_labels)]

    return vehicles

# Step 3: implement trackers

trackers = cv2.MultiTracker_create()

def update_trackers(frame):

    ret, boxes = trackers.update(frame)

    return boxes

def initialize_trackers(frame, vehicles):

    for _, det in vehicles.iterrows():

        bbox = tuple(det[['xmin', 'ymin', 'xmax', 'ymax']])

        # OpenCV boxes: (x, y, w, h)

        x1, y1, x2, y2 = map(int, bbox)

        w = x2 - x1

        h = y2 - y1

        tracker = cv2.TrackerCSRT_create()

        trackers.add(tracker, frame, (x1, y1, w, h))

# Step 4: visualize tracking results

while True:

    frame = ... # Get latest frame from frame grabber

    vehicles = detect_vehicles(frame)

    if trackers.getObjects().empty():

        initialize_trackers(frame, vehicles)

    else:

        boxes = update_trackers(frame)

        for i, box in enumerate(boxes):

            x, y, w, h = [int(v) for v in box]

            cv2.rectangle(frame, (x, y), (x + w, y + h), (255,0,0), 2)

            cv2.putText(frame, f'Vehicle {i+1}', (x, y-10), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0,255,0), 2)

    cv2.imshow('Vehicle Tracking', frame)

    if cv2.waitKey(1) & 0xFF == ord('q'):

        break

graph.stop()

cv2.destroyAllWindows()



Sunday, July 20, 2025

 This is a summary of the book titled “Rise of the Rest” written by Steve Case and published by Avid Reader Press in 2022. Steve dedicates this book to the Rise-of-the-Rest initiative which is a nationwide undertaking designed to shine spotlight on innovation growth outside of the traditional coastal tech hubs through city visits, hubs and events and capital investments. Communities across the United States and not just Silicon Valley are nurturing new businesses. He describes innovation competitions and showcases in US cities that don’t usually make the list of innovation hubs. His initiative forms a support network for innovation start-ups to succeed while encouraging communities to leverage their local strengths. In return, the startups can revitalize their stagnant communities. Adversity can also help and startups may benefit from diversity and sustainability.

Rise of the Rest is an initiative that encourages cities across the US to invest in innovation. It brings together innovators, government representatives, potential funders, business leaders, and policymakers to support innovative entrepreneurs in America's heartland. Each Rise of the Rest Road Trip visits five cities in five days, investing at least $500,000 in local start-ups through pitch competitions and innovation showcases. Success by a start-up firm is rare and requires essential resources such as connections, money, attention, and support from credible partners. Rise of the Rest can spark change in local communities it visits, such as those in Dallas, Birmingham, Louisville, and Chattanooga and Memphis. Business conditions increasingly favor startup growth in cities beyond Silicon Valley, with half of venture capital investment in the US going to California companies. Supportive networks help start-ups succeed, and communities benefit when they create innovation hubs. Examples of successful ecosystems include Louisville's Muhammad Ali Center and Madison's Warehouse District.

Rise of the Rest encourages communities to leverage their local strengths to support entrepreneurs and innovation. In the United States, cities should capitalize on their local assets, such as internet access, regional freight industry, and retail expertise. Atlanta's Opportunity Hub (OHUB) supports underrepresented innovators, while Denver's supportive government, academic, and business communities, sense of local cohesion, and business leaders' commitment to entrepreneurship make it fertile ground. Pennsylvania towns, like Pittsburgh, are applying similar tactics to create a technology hub, with robotics boosting Pittsburgh. The University of Nebraska-Lincoln Innovation Campus is turning investors and mentors into start-ups, like LifeLoop, a successful start-up that coordinates among senior living community residents, staff, and family members. Salt Lake City, Utah, has shifted its business culture from working and commuting to a consolidated live, work, play approach, with a focus on entrepreneurship skills and a focus on poverty alleviation. Nashville, Tennessee, demonstrates the value of tapping into local resources and fostering a supportive environment for start-ups.


#codingexercise: https://1drv.ms/w/c/d609fb70e39b65c8/ES9sIfBashZAtTf1RNl4_08BVrvP2mlGaE8YpS9rbMXE7g?e=Guah74

 #Codingexercise: https://1drv.ms/w/c/d609fb70e39b65c8/ES9sIfBashZAtTf1RNl4_08BVrvP2mlGaE8YpS9rbMXE7g?e=psuqdn

Saturday, July 19, 2025

 This is a summary of the book titled “AI In business and economics” written by Michael Vogelsang and Isabel Lausberg and published by De Gruyter in 2024. The authors present a collection of papers from the 2023 Economic perspective of Artificial Intelligence conference. The studies examine both the present and the future of AI adoption and impact on society and industry. Early adopters especially the large companies have the chance to establish monopolies while medium-sized companies need more gradual and measurable progress on the roadmap to integrate AI. Technology teams are not strictly required for AI adoption and AI can’t replace every job. Media plays a crucial role in its perception while AI can step up to provide guidance in ambiguous forecasts. 

Richard von Maydell of ETH Zürich and Christoph Menzel of the Federal Ministry for Economic Affairs and Climate Action, Berlin, discuss the rise of tech giants like Apple, Amazon, and Microsoft, noting that their reliance on information and communication technology (ICT) has led to increased market concentration. The growing use of AI in these sectors exacerbates the problem, as AI helps companies reduce costs and become more efficient, making it harder for new businesses to compete. This concentration of power can harm competition and push up prices, limiting consumer choices. 

The book also explores the increasing role of intangible assets like software, data, and AI itself, which further contribute to market concentration. Dominant companies thrive while smaller competitors struggle, leading to an imbalance that hurts consumers by limiting choices and contributing to high prices. Governments are urged to update laws and policies to regulate competition in digital markets. The European Union's Digital Markets Act is one such example, though its effectiveness in handling AI's growing influence remains uncertain. Financial support for smaller businesses and encouraging data sharing could help create a more level playing field. 

Medium-sized companies need a gradual, specific, and goal-oriented process for integrating AI. The KI-AGIL research project, led by Markus Feld, Wolfgang Arens-Fischer, and Marcel Schumacher of the University of Applied Sciences Osnabrück, aimed to help SMEs integrate AI into their operations. The project guided six SMEs through manageable phases called “sprints,” allowing them to build their AI systems gradually. Agile development techniques were used, focusing on flexibility and continuous improvement. This incremental approach helped businesses manage the complexity and reduce the risks of adopting AI. 

Isabel Lausberg, Arne Eimuth, and Anne Stockem Novo of the Ruhr West University of Applied Sciences discuss the slow adoption of AI in management reporting. Despite AI's potential to enhance these processes, companies face hurdles like poor data integration and the continued use of traditional tools like Excel. Improving data management, building a strong AI infrastructure, and fostering a culture of AI acceptance within organizations are crucial steps. Top management support is essential to drive the necessary changes and allocate resources. 

The WIRKsam project, a joint initiative by researchers from various universities, emphasizes the use of “plug and play” AI tools that require little to no coding experience. No-code and low-code approaches allow users to build applications without extensive programming knowledge. These tools make AI accessible to a wide range of users, enabling businesses to benefit from AI without relying solely on specialized tech teams. 

Ed Dandalt of Wilfrid Laurier University argues that AI is unlikely to replace physicians' roles in health care due to the unique combination of clinical and management skills they bring. While AI can support doctors by automating administrative tasks, it cannot replace the essential components of empathy, active listening, and decision-making critical to patient care. Patients' preferences for human physicians and the high demand for medical professionals further support this view. 

Simone Roth and Medina Klicic of Ruhr West University of Applied Sciences highlight the media's crucial role in shaping public perceptions of AI. Positive articles often emphasize AI's benefits, while negative coverage gains more attention, contributing to public concerns and skepticism. Addressing consumer-related issues like fairness and protection in the media could help mitigate fears and foster greater acceptance of AI technologies. 

Finally, Katharina I. Köstner, Bàrbara Llacay, and David Alaminos of the Universitat de Barcelona discuss the promise of algorithms like Deep Autoregressive Recurrent Networks (DeepAR) in forecasting market volatility. AI models using DeepAR outperform traditional methods in terms of accuracy and error reduction, particularly during periods of high volatility. Incorporating more control variables could enhance the model's accuracy and versatility in real-world applications. 

Overall, the book provides a comprehensive overview of AI's impact on business and economics, highlighting both opportunities and challenges. It emphasizes the need for gradual integration, regulatory updates, and the importance of human elements in AI adoption. The insights from various experts offer valuable guidance for businesses and policymakers navigating the complex landscape of AI. 

Friday, July 18, 2025

 In Strategic: The Skill to Set Direction, Create Advantage, and Achieve Executive Excellence, Rich Horwath delivers a compelling guide for leaders aiming to forge clear, differentiated strategies and drive lasting success. At its core, the book emphasizes that strategy is not just a plan—it’s a deliberate set of choices about where to play and how to win, rooted in clarity and trade-offs. Horwath argues that imitation, particularly competing on price or replicating a rival’s approach, is a weak substitute for true strategic thinking. Instead, he champions distinctiveness—identifying and nurturing what sets your organization apart.

He Introduces the GOST Framework—goals, objectives, strategy, and tactics—clarifying how high-level aspirations translate into specific, actionable plans. A goal is broad, an objective is precise, strategy is conceptual, and tactics are tangible. His Rule of Touch offers a practical litmus test: if you can physically touch it, it’s a tactic, not strategy.

Horwath critiques the widespread reliance on annual resource reallocation, highlighting that continuous and agile reassessment is key to performance. Through research with major organizations, he shows that timely resource shifts fuel growth and innovation. Concentration, not diversification, is where greatness often lies. Drawing on examples like Apple, Google, and Amazon, he reveals how focusing on core strengths rather than spreading thin leads to market leadership.

The book also explores strategic pitfalls—indecisiveness, risk aversion, and failure to innovate—arguing that the worst mistake isn’t making the wrong choice, but making none at all. Emotional intelligence emerges as another pillar of leadership: self-awareness and relationship management boost communication, resilience, and overall effectiveness.

Horwath pays close attention to organizational culture, suggesting that purpose-driven declarations (mission, vision, values) should steer behaviors and decisions. Toxic influences—such as persistent negativity or blame—must be rooted out to preserve strategic integrity. Effective culture, much like strategy, doesn’t happen by accident; it must be designed, reinforced, and protected.

On planning, he urges leaders to balance short-term tactics with long-term vision. Despite widespread reliance on one-year plans, imagining your company years down the road enhances adaptability and foresight. Talent management plays a key role here: hiring based on past behavior, not just experience, ensures stronger team performance.

Finally, Horwath encourages an open-minded, question-driven approach to innovation. Strategic options shouldn’t be treated as either/or propositions; instead, elements from different paths can be fused. He champions collaborative environments while warning against unproductive meetings and the myth of multitasking, advocating for structured, focused sessions and monotasking for clarity and impact.

Through vivid examples, thoughtful frameworks, and sharp insights, Horwath guides readers to build businesses that are strategically sound, culturally vibrant, and constantly evolving. His message is clear: strategic excellence isn’t reserved for the few—it’s a skill that can be cultivated, executed, and mastered.

#codingexercise: https://1drv.ms/w/c/d609fb70e39b65c8/EffYN3c80HpIg4GMHe5H--EBQLfEIZId0sDkkyfLjD8bwA?e=wrlieq