Tag Archives: machinelearning

Busy building my new workspace but meanwhile I am playing with machine learning

I needed more space for my business, so I moved to my big workshop space where our music studio was.

I’ve installed Yolo (v8) and generated an image using ChatGPT with many objects.

Installing Yolo:
See https://docs.ultralytics.com/quickstart/#install-ultralytics

Generated image

Using below python script I get a text file with hits and an image with objectboxes.

import cv2
import random
from ultralytics import YOLO
# Load YOLOv8 model
model = YOLO('yolov8n.pt')  
input_image_path = 'input.jpg'
image = cv2.imread(input_image_path)
def get_random_color():
    return [random.randint(0, 255) for _ in range(3)]
class_colors = {i: get_random_color() for i in range(len(model.names))}
results = model(input_image_path)
output_txt_path = 'output.txt'
with open(output_txt_path, 'w') as f:
    for result in results:
        for box in result.boxes:
            cls = int(box.cls[0])  
            confidence = box.conf[0].item() 
            bbox = box.xyxy[0].cpu().numpy()
            class_name = model.names[cls]
            # Write text file
            f.write(f"Class: {class_name}, Confidence: {confidence:.2f}, BBox: {bbox}\n")
            color = class_colors[cls]
            cv2.rectangle(image, 
                          (int(bbox[0]), int(bbox[1])), 
                          (int(bbox[2]), int(bbox[3])), 
                          color, 3)  # Thicker rectangle
            label = f'{class_name} {confidence:.2f}'
            font_scale = 1.0  # Larger font size
            font_thickness = 2  # Thicker font
            cv2.putText(image, 
                        label, 
                        (int(bbox[0]), int(bbox[1]) - 10), 
                        cv2.FONT_HERSHEY_SIMPLEX, 
                        font_scale, color, font_thickness)
output_image_path = 'output_with_boxes.jpg'
cv2.imwrite(output_image_path, image)
print(f"Detected objects saved to {output_txt_path}")
print(f"Output image with boxes saved to {output_image_path}"

Text file

Class: car, Confidence: 0.91
Class: car, Confidence: 0.90
Class: giraffe, Confidence: 0.90
Class: car, Confidence: 0.87
Class: bicycle, Confidence: 0.85
Class: person, Confidence: 0.77
Class: person, Confidence: 0.68
Class: bus, Confidence: 0.66
Class: sheep, Confidence: 0.64
Class: zebra, Confidence: 0.62
Class: umbrella, Confidence: 0.60
Class: bicycle, Confidence: 0.56
Class: umbrella, Confidence: 0.54
Class: airplane, Confidence: 0.52
Class: person, Confidence: 0.51
Class: person, Confidence: 0.48
Class: bicycle, Confidence: 0.44
Class: person, Confidence: 0.43
Class: stop sign, Confidence: 0.40
Class: umbrella, Confidence: 0.39
Class: motorcycle, Confidence: 0.39
Class: bicycle, Confidence: 0.38
Class: person, Confidence: 0.37
Class: person, Confidence: 0.35
Class: teddy bear, Confidence: 0.29
Class: truck, Confidence: 0.27
Class: airplane, Confidence: 0.26
Class: bus, Confidence: 0.25
Class: person, Confidence: 0.25

Code for real-time detection using a webcam.

from ultralytics import YOLO
import cv2
import math 
# start webcam
cap = cv2.VideoCapture(0)
cap.set(3, 640)
cap.set(4, 480)
# model
model = YOLO("yolo-Weights/yolov8n.pt")
# object classes
classNames = ["person", "bicycle", "car", "motorbike", "aeroplane", "bus", "train", "truck", "boat",
              "traffic light", "fire hydrant", "stop sign", "parking meter", "bench", "bird", "cat",
              "dog", "horse", "sheep", "cow", "elephant", "bear", "zebra", "giraffe", "backpack", "umbrella",
              "handbag", "tie", "suitcase", "frisbee", "skis", "snowboard", "sports ball", "kite", "baseball bat",
              "baseball glove", "skateboard", "surfboard", "tennis racket", "bottle", "wine glass", "cup",
              "fork", "knife", "spoon", "bowl", "banana", "apple", "sandwich", "orange", "broccoli",
              "carrot", "hot dog", "pizza", "donut", "cake", "chair", "sofa", "pottedplant", "bed",
              "diningtable", "toilet", "tvmonitor", "laptop", "mouse", "remote", "keyboard", "cell phone",
              "microwave", "oven", "toaster", "sink", "refrigerator", "book", "clock", "vase", "scissors",
              "teddy bear", "hair drier", "toothbrush"
              ]
while True:
    success, img = cap.read()
    results = model(img, stream=True)
    # coordinates
    for r in results:
        boxes = r.boxes
        for box in boxes:
            # bounding box
            x1, y1, x2, y2 = box.xyxy[0]
            x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2) # convert to int values
            # put box in cam
            cv2.rectangle(img, (x1, y1), (x2, y2), (255, 0, 255), 3)
            # confidence
            confidence = math.ceil((box.conf[0]*100))/100
            print("Confidence --->",confidence)
            # class name
            cls = int(box.cls[0])
            print("Class name -->", classNames[cls])
            # object details
            org = [x1, y1]
            font = cv2.FONT_HERSHEY_SIMPLEX
            fontScale = 1
            color = (255, 0, 0)
            thickness = 2
            cv2.putText(img, classNames[cls], org, font, fontScale, color, thickness)
    cv2.imshow('Webcam', img)
    if cv2.waitKey(1) == ord('q'):
        break
cap.release()
cv2.destroyAllWindows()

Note: generated picture is not perfect. See zebra. AI output is affected by this.

Arduino Tiny Machine Learning Kit

A while ago I bought a little machine learning kit.

I’ve been reading at listening to ML podcasts and websites.

One on Spotify I liked was:

Also, the following Coursera was interesting
https://www.coursera.org/learn/machine-learning

I’ve been testing using Python on my Laptop.
(see other posts)

And a camera with esp32 using face detection.

See here multiple posts about these experiments.

https://www.henriaanstoot.nl/tag/machinelearning/

Today the first experiments using this kit.

  • Arduino Nano 33 BLE Sense board
  • OV7675 Camera
  • Arduino Tiny Machine Learning Shield
  • USB A to Micro USB Cable
  • 9 axis inertial sensor: what makes this board ideal for wearable devices
  • humidity, and temperature sensor: to get highly accurate measurements of the environmental conditions
  • barometric sensor: you could make a simple weather station
  • microphone: to capture and analyse sound in real time
  • gesture, proximity, light color and light intensity sensor : estimate the room’s luminosity, but also whether someone is moving close to the board
  • Microcontroller nRF52840
  • Operating Voltage 3.3V
  • Input Voltage (limit) 21V
  • DC Current per I/O Pin 15 mA
  • Clock Speed 64MHz
  • CPU Flash Memory 1MB (nRF52840)
  • SRAM 256KB (nRF52840)
  • EEPROM none
  • Digital Input / Output Pins 14
  • PWM Pins all digital pins
  • UART 1
  • SPI 1
  • I2C 1
  • Analog Input Pins 8 (ADC 12 bit 200 ksamples)
  • Analog Output Pins Only through PWM (no DAC)
  • External Interrupts all digital pins
  • LED_BUILTIN 13
  • USB Native in the nRF52840 Processor
  • IMU LSM9DS1 (datasheet)
Gesture test ( yes on a windows surface tablet, but Vincent and I installed linux on it!)

I just started and will update this page, with other experiments.

Note: displaying Arduino output without installing the IDE

stty -F /dev/ttyACM0 raw 9600
cat /dev/ttyACM0
................................
................................
................................
................................
................................
................................
................................
................................
................................
................####............
...............##..#............
..............##...##...........
..............#.....#...........
..............###...#...........
..............##.....#..........
..............##.....#..........
...............#....##..........
...............######...........
................................
................................
................................
................................

I asked ChatGPT to write code for a Crack the Code game.

The Game

elcome to Crack the Code!
The code has 4 digits. Each digit is between 0 and 9.
You have 10 tries to guess the code.
After each guess, you will be told how many digits you got correct and how many were misplaced.
Good luck!
Enter your guess: 1234
Guess 1: 1234 - 0 correct, 0 misplaced
Enter your guess: 5678
Guess 2: 5678 - 2 correct, 1 misplaced
Enter your guess: 5689
Guess 3: 5689 - 1 correct, 2 misplaced
Enter your guess: 9688 
Guess 4: 9688 - 1 correct, 2 misplaced
....
Sorry, you did not crack the code. The code was 5568.

Generated python code

import random

def generate_code():
    code = ""
    for i in range(4):
        code += str(random.randint(0, 9))
    return code

def check_guess(code, guess):
    correct_digits = 0
    misplaced_digits = 0
    for i in range(4):
        if guess[i] == code[i]:
            correct_digits += 1
        elif guess[i] in code:
            misplaced_digits += 1
    return (correct_digits, misplaced_digits)

def play_game():
    print("Welcome to Crack the Code!")
    print("The code has 4 digits. Each digit is between 0 and 9.")
    print("You have 10 tries to guess the code.")
    print("After each guess, you will be told how many digits you got correct and how many were misplaced.")
    print("Good luck!")
    code = generate_code()
    num_guesses = 0
    while num_guesses < 10:
        guess = input("Enter your guess: ")
        if len(guess) != 4 or not guess.isdigit():
            print("Invalid input. Please enter a 4-digit number.")
            continue
        num_guesses += 1
        correct, misplaced = check_guess(code, guess)
        print(f"Guess {num_guesses}: {guess} - {correct} correct, {misplaced} misplaced")
        if correct == 4:
            print("Congratulations! You cracked the code!")
            return
    print(f"Sorry, you did not crack the code. The code was {code}.")

play_game()

After this i let the AI made some changes, which kinda worked.
Scary nevertheless

ChatGPT IA generated chats/texts

ChatGPT is a large language model chatbot developed by OpenAI based on GPT-3.5. It has a remarkable ability to interact in conversational dialogue form and provide responses that can appear surprisingly human.

So i asked stuff like:

Thats a pretty good joke about a 6502 CPU. I tought it would be a hard question

The question : “what are women looking for in a man”
Gave a lot of lines :
No generic answer
Lines with Respect, Humor, Kindness, Confidence

Then it stopped with:

Typical .. even AI is baffled by women.

Below a chat session from a great friend of mine Tyrone

Tyrone: Ok could you write me a commodore c64 assembly routine to make the screen white?

Certainly! Here is a simple assembly routine that will clear the screen and set all of the pixels to white on a Commodore 64:

Copy code

ldx #$00
ldy #$00 
clear_screen: 
sta $d800,x 
inx 
cpx #$40 
bne clear_screen 

This code will loop through all of the screen memory locations and set the value at each location to $00, which will turn all of the pixels white. The inx and iny instructions are used to increment the X and Y registers, which are used to keep track of the current screen position. The cpy instruction is used to compare the value in the Y register to $20, and the bne instruction is used to branch back to the clear_screen label if the values are not equal, causing the loop to continue until the entire screen has been cleared.

I hope this helps! Let me know if you have any questions.



Tyrone: Well a big code but i wanted to make the screen the color white not with a character space .. this is also white but it's more logical that the color is named then the white space this code should be three lines of could can you do this?

Certainly! Here is a simple assembly routine that will clear the screen and set all of the pixels to white on a Commodore 64:

Copy code

ldx #$00
ldy #$00 
clear_screen: 
sta $d800,x 
inx 
cpx #$40 
bne clear_screen 

This code will loop through all of the screen memory locations and set the value at each location to $ff, which will turn all of the pixels white. The inx instruction is used to increment the X register, which is used to keep track of the current screen position. The cpx instruction is used to compare the value in the X register to $40, and the bne instruction is used to branch back to the clear_screen label if the values are not equal, causing the loop to continue until the entire screen has been cleared.

I hope this helps! Let me know if you have any questions.



Tyrone: This is wrong here is a hint use registers d020 and d021 for screen and border color 

I apologize for the mistake in my previous response. Here is the corrected code that will set the screen and border colors to white on a Commodore 64:

Copy code

lda #$0f
sta $d020
sta $d021 

This code will store the value $0f in the memory locations at $d020 and $d021, which are the registers that control the screen and border colors on the Commodore 64. The value $0f corresponds to the color white.

I hope this helps! Let me know if you have any questions.



Thank you ai 

HuskyLens Test

I’ve bought a Husky lens recently. It was very cheap for what you get.
(50 Euro’s)
The first tests are promising.

Nice little, but powerful gadget.
https://wiki.dfrobot.com/HUSKYLENS_V1.0_SKU_SEN0305_SEN0336

Cables to connect Rpi or Arduino, mounts, Huskylens and Protectioncover (sold separately)
  • face recognition
  • object tracking
  • object recognition
  • line tracking
  • color recognition
  • tag recognition
  • object classification

Communication can be done via I2C and Uart.
Uses a sdcard to store learning data.
Has white leds for object lighting.

Build-in objects which are recognised out of the box. (Others can be learned by the device)

aeroplane, bicycle, bird, boat, bottle, bus, car, cat, chair, cow, dining-table, dog, horse, motorbike, person, potted plant, sheep, sofa, train, TV

Photo manager addition using ML!

A few years ago i wrote a photo manager .. again .. ( see post about my first previous photo manager )
It is a web gui to find photos in my huge photo archive.
I manually added 190k tags to 120k photos in 20+ years.

I thought wouldn’t it be nice if i can generate additional metadata using Machine Learning. A few years ago i did some testing and followed a podcast and free course about machine learning.

So today i started to implement a addition to my gui. Machine recognition tags!

It already kinda works.

Things to do :

  • Make it a background job, my fileserver doesn’t run Tensorflow on a GPU, so it is slooow
  • Embed in existing GUI and stats
  • Design a editor to remove wrong tags

Below a part of ML images

Command to get a thumbnail sheet with only directory names:

montage -verbose -units PixelsPerInch -density 300 -tile 7x6 -label "%d" -font Arial -pointsize 6 -background "#FFFFFF" -fill "black" -define jpeg:size=253x154 -geometry 253x154+2+2 -auto-orient */*.JPG -title "ML Thumbs" thumbsheet.jpg

Maybe, i can use debug output like below.

['lakeside, lakeshore (score = 0.47934)', 'seashore, coast, seacoast, sea-coast (score = 0.11385)', 'sandbar, sand bar (score = 0.08822)', 'breakwater, groin, groyne, mole, bulwark, seawall, jetty (score = 0.06281)', 'valley, vale (score = 0.01790)', '']

Machine Learning

Today i started with Coursera’s Machine Learning course.

My friend aloha is doing interesting stuff with ML, but recently i’ve been interested in a work related ML project.

Besides this course i’m following a spotify Podcast called “Machine Learning Guide”, i listen to this on my way to work and back.

I’ve been playing with a lot of code after that. Luckily there are many ebooks about this subject.

  • One of the first was a python program wich used the length of a person and shoesize to determine if it was a man or a woman
  • Another fun one was a program with could determine if a wine was red or white only based by a description
  • There are several graphic based programs i’ve tried. Deepfake, 8mm film enhancers, image classifiers, openface
  • For sound there was voice cloner to test. And audio to text (which i used to transcribe old cassette tapes and VHS tapes.

UPDATE: In 2022 i used what i have learned to enrich my photo metadata.
https://www.henriaanstoot.nl/2022/05/29/photo-manager-addition-using-ml/