Skip to content

Instantly share code, notes, and snippets.

@nomissbowling
Created May 15, 2022 07:25
Show Gist options
  • Select an option

  • Save nomissbowling/0f53085a5815736ddb8e43d73059d1a0 to your computer and use it in GitHub Desktop.

Select an option

Save nomissbowling/0f53085a5815736ddb8e43d73059d1a0 to your computer and use it in GitHub Desktop.
OpenCV starting point for different cameras
import cv2 #Open CV 3.4.1
import datetime
import socket
import time
#get the cam.....
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
#for tello
def send_command(command):
sock.sendto(command.encode('utf-8'), ("192.168.10.1", 8889))
send_command("command")
time.sleep(5)
send_command("streamon")
time.sleep(5)
### Here are the different cameras. I had a lot of trouble finding out how to receive their different streams
### so here it is. webcam, Tello drone, Rasp Pi, and an app on an IP Camera app on my iPhone.
### if you're going to use raspivid on the Pi and you have RPI_Cam_Web_Interface installed you need to stop the camera
### first or it will throw you an error similar to a camera not being detected. You could do this in the browser app.
### If you're going to use the tello feed, you need to have FFMPEG installed, I would recommend building it from source
### instead of going from homebrew. You also don't need FFPlay. There is an events function written in here so you can do
### what you want with event handlers and a datetime.now() thrown in a PutText. This is good to start all OpenCV projects.
### The only thing you might need to change is the window of the video. Some are larger than others but that's an easy fix
#uncomment whichever camera you want to use
cap = cv2.VideoCapture(0) #from the Macbook WebCamera
# from ios IP Cam app....
#cap = cv2.VideoCapture("rtsp://peanutbutter:jelly@192.168.86.21:8554/live")
##from the Tello camera
#cap = cv2.VideoCapture("udp://@0.0.0.0:11111", cv2.CAP_FFMPEG)
#from raspberry pi camera .... raspivid -t 0 -l -o tcp://0.0.0.0:3333
#cap = cv2.VideoCapture("tcp://192.168.86.248:3333")
#ret, frame = cap.read()
# Add Events
def click_event(event, x, y, flags, param):
#if event == cv2.EVENT_LBUTTONDOWN:
print("The event is : " + event)
cv2.namedWindow("The Frame")
cv2.setMouseCallback("The Frame", click_event)
while True:
# to grayscale
#gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
ret, frame = cap.read()
# Add text ...
font = cv2.FONT_HERSHEY_SIMPLEX
text = datetime.datetime.now()
frame = cv2.putText(frame, str(text), (25, 450), font, 1, (0, 0, 255), 2, cv2.LINE_AA)
cv2.imshow("The Frame", frame)
#ret, frame = cap.read()
if cv2.waitKey(1) & 0xFF == ord('q'):
break
send_command("streamoff") #for tello
cap.release()
cv2.destroyAllWindows()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment