Skip to content

Instantly share code, notes, and snippets.

@Merwanski
Created May 12, 2022 19:43
Show Gist options
  • Save Merwanski/4dc70d3d05efd30541e1c5cb7dded520 to your computer and use it in GitHub Desktop.
Save Merwanski/4dc70d3d05efd30541e1c5cb7dded520 to your computer and use it in GitHub Desktop.
opencv-python_connect_to_android_camera_via_rstp
import cv2
import numpy as np
from matplotlib import pyplot as plt
import os
"""
source: https://medium.com/@nhancv/opencv-python-connect-to-android-camera-via-rstp-9eb78e2903d5
NB: Did not work on windows 10
"""
os.environ["OPENCV_FFMPEG_CAPTURE_OPTIONS"] = "rtsp_transport;0"
url_stream = "rtsp://freja.hiof.no:1935/rtplive/definst/hessdalen03.stream"
# url_stream = "http://192.168.1.18:4747/video"
# vcap = cv2.VideoCapture("rtsp://192.168.80.28:5554/camera", cv2.CAP_FFMPEG)
vcap = cv2.VideoCapture(url_stream, cv2.CAP_FFMPEG)
while(1):
ret, frame = vcap.read()
if ret == False:
print("Frame is empty")
break;
else:
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# remove noise
img = cv2.GaussianBlur(gray,(3,3),0)
# convolute with proper kernels
laplacian = cv2.Laplacian(img,cv2.CV_64F)
sobelx = cv2.Sobel(img,cv2.CV_64F,1,0,ksize=5) # x
sobely = cv2.Sobel(img,cv2.CV_64F,0,1,ksize=5) # y
"""
plt.subplot(2,2,1),plt.imshow(img,cmap = 'gray')
plt.title('Original'), plt.xticks([]), plt.yticks([])
plt.subplot(2,2,2),plt.imshow(laplacian,cmap = 'gray')
plt.title('Laplacian'), plt.xticks([]), plt.yticks([])
plt.subplot(2,2,3),plt.imshow(sobelx,cmap = 'gray')
plt.title('Sobel X'), plt.xticks([]), plt.yticks([])
plt.subplot(2,2,4),plt.imshow(sobely,cmap = 'gray')
plt.title('Sobel Y'), plt.xticks([]), plt.yticks([])
plt.show()
"""
cv2.imshow('sobelx', sobelx)
cv2.imshow('VIDEO', frame)
cv2.waitKey(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment