AI USB Camera:Install Required Packages in Ubuntu
- Setting up your AI USB camera in the Ubuntu environment.
- Integrating and utilizing the modern YOLOv8 model for real-time object detection.
- Creating your very own Python script that connects everything together.
Object Detection Using USB Camera in Ubuntu – YOLOv8 Recognizes Objects in Real Time
1. Installing the required packages
First, it is necessary to install the required libraries on your system. Open the terminal and enter the following commands:
sudo apt update
sudo apt install python3-pip
pip install ultralytics opencv-python
If the installation fails, you can try to force the installation with a potential risk of affecting system packages (use with caution):
pip install ultralytics opencv-python --break-system-packages
Then update the pip package manager:
pip install --upgrade pip
or
pip install --upgrade pip --break-system-packages
or
This will install the YOLOv8 (ultralytics) library and OpenCV library for working with images and video.
2. Verify the YOLO installation
To make sure YOLOv8 is installed correctly, run this command in the terminal:
yolo
If you see help with available YOLO commands, the installation was successful ✅
3. Downloading a pretrained model and first detection
YOLOv8 offers several versions of models – from lightweight to the most accurate ones. For the beginning, we recommend the yolov8n.pt (nano) model, which is very fast:
yolo detect predict model=yolov8n.pt source='0'
This command will:
• download the yolov8n.pt model
• turn on your default camera
• display detected objects in real time 📸
Note: If you have multiple cameras, try source='1' or a higher number.
4. Creating your own Python script
For advanced usage, you can create your own Python script. Below is an example:
This script loads the model, starts the camera and in an infinite loop performs detection on each frame. The results are displayed in a separate window. Press q to exit the program.
The YOLOv8 model can recognize a wide range of objects – people, dogs, horses, cars, birds, skateboards and many others. The accuracy is significantly higher than older models such as MobileNet SSD.
Optional: switch to larger models
If you have more powerful hardware, you can use more accurate models:
• yolov8s.pt – small
• yolov8m.pt – medium
• yolov8l.pt – large
• yolov8x.pt – extra large (most accurate but slowest)
In the Python script, simply change:
model = YOLO("yolov8s.pt")
Frequently Asked Questions and Troubleshooting
⚠️ Warning “Could not initialize NNPACK!”
This is not an error. You just don’t have CPU optimizations for NNPACK → YOLO continues to work, only slightly slower.
Black screen after starting the camera
Possible reasons:
1️⃣ Camera is being used by another program
Close applications using the camera.
Check processes:
sudo fuser -v /dev/video0
2️⃣ Verify camera functionality using this script:
test-1
from ultralytics import YOLO
import cv2
model = YOLO("yolov8n.pt")
cap = cv2.VideoCapture(0)
if not cap.isOpened():
print("❌ Camera failed to open")
exit()
while True:
ret, frame = cap.read()
if not ret:
print("❌ Camera returns no frame")
break
results = model.predict(source=frame, conf=0.5, verbose=False)
annotated_frame = results[0].plot()
cv2.imshow("YOLO Camera", annotated_frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
More tips for black screen:
• change 0 → 1 in VideoCapture()
• set resolution:
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
• add a small delay:
cv2.waitKey(10)
Older laptops ↴
If the hardware is weak, switch YOLO to CPU:
model.to('cpu')
Tips for speeding up:
• smaller frames (e.g. imgsz=320)
• CPU only (default)
• acceleration via TFLite / ONNX
Your final test file ✅
Designed for weak PCs with integrated GPU, e.g. 2nd generation i3. Should also work on Raspberry Pi 4.
# yolo_stream_light.py
from ultralytics import YOLO
import cv2
import time
model = YOLO("yolov8n.pt")
model.to("cpu")
cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 416) # 416 often faster
# cap.set(cv2.CAP_PROP_FRAME_WIDTH, 960)
# cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)
if not cap.isOpened():
print("Camera cannot be opened")
exit()
print("Starting stream. Press q to exit.")
while True:
ret, frame = cap.read()
if not ret:
print("Cannot load frame")
break
results_gen = model.predict(source=frame, conf=0.35, imgsz=416,
verbose=False, stream=True)
try:
res = next(results_gen)
except StopIteration:
res = None
if res is not None:
annotated = res.plot()
else:
annotated = frame
cv2.imshow("YOLO light stream", annotated)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
Save the file with the .py extension and with no spaces in the name:
✅ yolo_stream_light.py
Open the terminal in the folder with your created program yolo_stream_light.py. Run the program in the terminal using the command: python3 yolo_stream_light.py













