2 Learn Python to A.I Programming – Lesson

Lesson 2 – Loops and Arrays (With A.I YOLO Example)

The goal of this lesson is to run real AI code using YOLOv8 and see how an AI model detects objects in an image.
In this example, the YOLO model analyzes a picture stored on your computer in the folder C:\AI\PICS, detects objects such as people, cows, and balls, and prints how many of each it finds.
You can save your test images into C:\AI\PICS and replace the file name in the code (for example, pic1.png) to analyze different images with artificial intelligence.

 remark Jupiter notebook – run this code to know where you locate 

 

we will use this code and learn how it works 

Lesson 2 to teach loops and arrays using a real YOLO-style example

Before we use YOLO-style data, let’s understand dictionaries and arrays with simple

Lesson – Dictionaries and Loops in Python

A dictionary in Python stores data as key → value pairs.

Output:
CARTi
AI Robot
1.0


2. Adding and Changing Values in a Dictionary

Output:
{'name': 'CARTi', 'type': 'AI Robot', 'version': 2.0, 'battery': '95%'}


3. Looping Through a Dictionary

Output:
name → CARTi
type → AI Robot
version → 2.0

4. Looping Through a 1-D Array (List)

Output:
Animal: cow
Animal: dog
Animal: cat
Animal: horse
Animal: bird

Output:
Loop index: 0
Loop index: 1
Loop index: 2
Loop index: 3
Loop index: 4


5. Loop with Index (using range and len)

Output:
Index: 0 | Animal: cow
Index: 1 | Animal: dog
Index: 2 | Animal: cat


Output

Row: [1, 2, 3]
Matrix value: 1
Matrix value: 2
Matrix value: 3
Row: [4, 5, 6]
Matrix value: 4
Matrix value: 5
Matrix value: 6
Row: [7, 8, 9]
Matrix value: 7
Matrix value: 8
Matrix value: 9

Explanation

The outer loop prints each full row of the matrix.
The inner loop then prints each element inside that row, one by one.
This helps visualize the 2-D structure — a key concept in A.I. for handling image pixels or data grids


esson 6 – Output (Text Only)

Example 6.1 – Output
Animal: cow
Animal: dog
Animal: cat

Example 6.2 – Output
Animal index: 0 | Name: cow
Animal index: 1 | Name: dog
Animal index: 2 | Name: cat

Example 6.3 – Output
Detected cow with confidence 0.92
Detected dog with confidence 0.85
Detected cat with confidence 0.88

Example 6.4 – Output
High confidence detection: cow (0.92)

Example 6.5 – Output
Detected: cow | Confidence: 0.92
Detected: dog | Confidence: 0.85
Detected: cat | Confidence: 0.88

Example 6.6 – Output
Matrix value: 1
Matrix value: 2
Matrix value: 3
Matrix value: 4
Matrix value: 5
Matrix value: 6
Matrix value: 7
Matrix value: 8
Matrix value: 9

Example 6.7 – Output
—- New Frame —-
Detected: cow | Confidence: 0.91
Detected: dog | Confidence: 0.87
—- New Frame —-
Detected: car | Confidence: 0.94
Detected: person | Confidence: 0.96

Example 6.8 – Output
Detection count: {'cow': 2, 'dog': 1, 'cat': 1}

Example 6.9 – Output
Filtered results: [{'cls': 'person', 'conf': 0.95}, {'cls': 'car', 'conf': 0.88}]

Example 6.10 – Output
Detected person | Confidence: 0.93
Location: (120, 200) → (260, 480)
Detected car | Confidence: 0.89
Location: (300, 150) → (600, 400)


example 7 : let see and understand A.I  yolo real  code

Program start…
image 1/1 C:\AI\PICS\pic1.png: 448×640 1 person, 3 cows, 1 sports ball, 427.0 ms
Speed: 20.1 ms preprocess, 427.0 ms inference, 22.9 ms postprocess per image at shape (1, 3, 448, 640)
Number of cows detected: 3

YOLO processed one image (C:\AI\PICS\pic1.png) resized to 448×640 px and detected 1 person, 3 cows, and 1 sports ball in 427 ms.
It spent 20.1 ms loading/resizing (preprocess), 427 ms detecting (inference), and 22.9 ms filtering results (postprocess).
The model input shape (1, 3, 448, 640) means 1 image, 3 color channels, and the resized dimensions.

Finally, your loop found and printed “Number of cows detected: 3.”


Program start…
image 1/1 C:\AI\PICS\pic1.png: 448×640 1 person, 3 cows, 1 sports ball, 128.1 ms
Speed: 2.3 ms preprocess, 128.1 ms inference, 1.5 ms postprocess per image at shape (1, 3, 448, 640)
Number of cows detected: 3

כתיבת תגובה