[closed] How to run a complex python code in Arduino without CPU ?

I am using Arduino Uno and a microSD card reader and I want to run a python code (without my PC connected) in that the code file will be in the microSD card

The issue is that the python uses interpreter to run.

I have a custom trained yolov8n.pt file and it runs with python file and a camera (temporarily webcam) and detects objects

main.py--

### pip install opencv-python ultralytics ###
from ultralytics import YOLO
import cv2
import math
import json
import os

cap = cv2.VideoCapture(0)
cap.set(3, 640)
cap.set(4, 480)

model = YOLO('best_money.pt')

#### Object classes
classNames = ['10 rupees', '100 rupees', '20 rupees', '200 rupees', '2000 rupees', '50 rupees', '500 rupees']

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_SCRIPT_SIMPLEX
            fontScale = 1
            color = (0, 255, 0)
            thickness = 2

            cv2.putText(img, classNames[cls], org, font, fontScale, color, thickness)

    cv2.imshow('Webcam', img)
    if cv2.waitKey(1) == 27:
        break

cap.release()
cv2.destroyAllWindows()

Welcome to the forum

I am afraid that what you want to do sounds impossible due to the architecture of the AVR processor used by the Uno

How is it that you find yourself needing to do that in such a hurry ?

The Uno won't run Python code. Adafruit sells MCUs that work with CircuitPython, a large C program that does the interpretation.

I am doing an object detection in arduino and the main code is in python so I wanted to do it.
is there any other way I can do this thing??
another way like getting a python interpreter or something?
(main thing is the python code should be running without a PC)

Write Arduino code in C/C++ that does the object detection. Actually, the code would simply read the results produced by whatever sensor is connected, a half-dozen lines or so.

As suggested by @jremington, there are MCUs that will run CircuitPython, but I doubt that it can be run from an SD card

C/C++ is generally "King of the Castle" in teh world of Arduino

ya but I have a custom trained yolov8n.pt model with different classes and I will keep it in microSD card and then a python file to run it

and as you said can you tell the C code similar to the python code that does the work?

ya but I have a custom trained yolov8n.pt model with different classes and the python file will run this thing with the help of an ESP32 cam

the main problem is that I dont know about C that much
also if the python file thing can be done in C language that would also be nice

Not with any Arduino.

yes with the arduino as the main CPU

Cool. Let us know when you have it working.

I want help related to how to do this thing that's why I came here
If anyone knows how to do this object detection thing in C language with arduino please provide the details

@Delta_G this thing (object detection)

I dont want to use RPi
(low cost thing)

nvm the link
this is the thing I as saying--
ya but I have a custom trained yolov8n.pt model with different classes and the python file will run this thing with the help of an ESP32 cam

the main problem is that I dont know about C that much
also if the python file thing can be done in C language that would also be nice

I mean that if someone knows how to do this thing they can provide me with some info.
and sorry for not giving deep info.----nvm---I've included the python code
let me give it here also-
main.py--

### pip install opencv-python ultralytics ###
from ultralytics import YOLO
import cv2
import math
import json
import os

cap = cv2.VideoCapture(0)
cap.set(3, 640)
cap.set(4, 480)

model = YOLO('best_money.pt')

#### Object classes
classNames = ['10 rupees', '100 rupees', '20 rupees', '200 rupees', '2000 rupees', '50 rupees', '500 rupees']

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_SCRIPT_SIMPLEX
            fontScale = 1
            color = (0, 255, 0)
            thickness = 2

            cv2.putText(img, classNames[cls], org, font, fontScale, color, thickness)

    cv2.imshow('Webcam', img)
    if cv2.waitKey(1) == 27:
        break

cap.release()
cv2.destroyAllWindows()

That standard example code will run on any RPi with a USB camera. Skip your next two or three cups of cappuccino and buy the RPi Zero W 2.

The Arduino Nano ESP32 will run Micropython a little over $20.
Arduino have a micropython port for the Nano and I believe CircuitPython also have a Nano port.
It would be down to you to see if you can run your libraries from the sd card or maybe flash

A PC has 4.000.000 kByte RAM CPU-Clock runs at 3000 MHz 64 bit = 192.000 MIPS
A raspberry py zero has 512.00 kByte RAM CPU-Clock runs at 1000 MHz 32 bit = 32.000 MIPS

An Arduino Uno has 2 kByte RAM CPU-Clock runs at 16 MHz 8 bit = 128 MIPS

Can you see from that NUMBERS what a

POOR TINY DWARF

an Arduino Uno is?

Even a small raspberry Pi Zero has 256.000 times more RAM

two-hundred-sixty-five-THOUSAND times more RAM

a small raspberry Pi Zero has 250 times more calculation-power

For running PC-usable PYTHON like python 3.9
you need megabytes of RAM = 1000 kB RAM

micropython for micro-controllers is what its name says

it is MICRO

micro-python does not even have all functions that will be nescessary to run your yolov8n.pt-model

So what does it need more to make you understand

There is a minimum RAM-requirement in the MEGA-Byte range (Arduino 0.002 MB)
There is a minimum calculation-power requirement in the 1000 MIPS range (Arduino 128 MIPS)
that is a MUST to make your yolov8n.pt -model run.

I estimate that your programming knowledge is at a low level. Otherwise you would not ask such questions.
It will take you 1000 to 3000 hours of learning C++ to be able to use a Megabyte microcontroller which costs $40 to re-create this yolov8n.pt model functionality

Now even the worst paid job of dish-washing will earn you more dollars in 100 hours than a raspberry Pi 4 costs.

Another option might be to buy a used smartphone which is 5 years old to use the smartphones camera and a object-detection-app to do your object detection.

Even this 5 year old smartphone will outperform a raspberry Pi zero because this smartphone will have a 2000 MHz 32 bit prozessor and 2000 MB RAM

best regards Stefan

@StefanL38 I think it's possible that @why_you_need_a_username is a troll pretending to be incredibly thick to get a rise out of you. Don't feed the trolls, my friend, don't feed the trolls.