public ip camera reading with opencv - python

I would like to process an public ip camera stream in a simple python application, but I can't get it to work. I am able to connect with the local stream using rtsp, but when I try to caputre the stream in the same fashion via ipcamlive.com I am not able to open the stream. I am using an Axis camera model M3105-L which streams h264.
This is the code for capturing the stream on my local network. The code actually doesn't matter. Only the url matters.
Theres an code example on how to embed the stream (<iframe src= "https://g0.ipcamlive.com/player/player.php?alias=2022camera1" width="800px" height="450px" frameborder="0" allowfullscreen> </iframe>) into your webpage, but I would like to process the stream with opencv as well to eventually do something with motion detection etc.
Any help would be appriciated. Thank you in advance.
import cv2
import os
RTSP_URL = 'rtsp://<username>:<password>#<ip-address>/axis-media/media.amp'
cap = cv2.VideoCapture(RTSP_URL, cv2.CAP_FFMPEG)
if not cap.isOpened():
print('Cannot open RTSP stream')
exit(-1)
while True:
_, frame = cap.read()
cv2.imshow('RTSP stream', frame)
if cv2.waitKey(1) == 27:
break
cap.release()
cv2.destroyAllWindows()

Related

The solution for Failed to send close message

I am using opencv to load a live video from youtube, but the program terminates in the middle with a "Failed to send close message".
I would like to keep getting images 24 hours a day, all the time, and I would like to know what is the best way to do this.
import pafy
import cv2
url = "https://www.youtube.com/watch?v=_9OBhtLA9Ig"
video = pafy.new(url)
best = video.getbest(preftype="mp4")
capture = cv2.VideoCapture(best.url)
while True:
grabbed, frame = capture.read()
# ...

how to capture and import RAW or .DNG image directly from an iPhone 13 on a mobile device?

I am attempting to capture or import RAW images directly from a small python script to import for further analysis. I am attempting to create a simple mobile code to run on pythnissta 3 or similar platform to stream line my analysis. I have tried a few things to import the last taken image form the photo library but my attempts were unfruitful:
import photos
all_assets = photos.get_assests()
last_asset = all_assests[-1]
img = last_assest.image()
img. show
To capture images directly:
import photos
import console
console.clear()
nom_album = contacts
photos.create_album(nom_album)
img = photos.capture_image()
Here's a link to an example image.This is a .DNG image taken under the Pro RAW settings from an iPhone 13 pro.

How to add "pytube" downloading info to tqdm Progress Bar?

I am trying make a YouTube video downloader.
I want make a progress bar while downloading the YouTube video but I cant get any info (how many MB have been downloaded or how many video MB).
I don't know if this is possible with python or not. Here is my code so far:
import time , os
from pytube import YouTube
from tqdm import tqdm
al = str(input("C4ommand:"))
if al == "4":
af = input("Link:")
you = YouTube(af)
try:
os.mkdir("Download")
except:
pass
time.sleep(2)
time.sleep(1)
res = you.streams.get_highest_resolution()
a = res.download()
with tqdm(total=100) as pbar:
for i in range(10):
time.sleep(0.5)
pbar.update(10)
print(af + "Link downloading....")
b = open("\Download", "w")
b.write(a)
b.close()
print("Downloaded")
To access the progress of the download, you can use the on_progress_callback argument when creating a YouTube instance.
The pytube quickstart says the following:
The on_progress_callback function will run whenever a chunk is downloaded from a video, and is called with three arguments: the stream, the data chunk, and the bytes remaining in the video. This could be used, for example, to display a progress bar.
from pytube import Stream
from pytube import YouTube
from tqdm import tqdm
def progress_callback(stream: Stream, data_chunk: bytes, bytes_remaining: int) -> None:
pbar.update(len(data_chunk))
url = "http://youtube.com/watch?v=2lAe1cqCOXo"
yt = YouTube(url, on_progress_callback=progress_callback)
stream = yt.streams.get_highest_resolution()
print(f"Downloading video to '{stream.default_filename}'")
pbar = tqdm(total=stream.filesize, unit="bytes")
path = stream.download()
pbar.close()
print(f"Saved video to {path}")
Sample output:
Downloading video to 'YouTube Rewind 2019 For the Record YouTubeRewind.mp4'
100%|██████████████████████████████| 87993287/87993287 [00:17<00:00, 4976219.51bytes/s]
Saved video to /tmp/testing/YouTube Rewind 2019 For the Record YouTubeRewind.mp4
Pytube has a built-in progress bar, but it does not use tqdm. Please see https://stackoverflow.com/a/60678355/5666087 for more information.

How can I render a requested image from Street View Static API using python?

I used requests:
import requests
url = 'https://maps.googleapis.com/maps/api/streetview?size=400x400&location=40.720032,-73.988354&heading=0&key=[YOUR KEY HERE]'
r = requests.get(url)
and I get this in r.contents (how do I turn these bytes to an image? I tried PIL and matplotlib but could not make it work):
b'\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x01\x00\x00\x01\x00\x01\x00\x00\xff\xdb\x00C\x00\x08\x06\x06\x07\x06\x05\x08\x07\x07\x07\t\t\x08\n\x0c\x14\r\x0c\x0b\x0b\x0c\x19\x12\x13\x0f\x14\x1d\x1a\x1f\x1e\x1d\x1a\x1c\x1c $.\' ",#\x1c\x1c(7),01444\x1f\'9=82<.342\xff\xdb\x00C\x01\t\t\t\x0c\x0b\x0c\x18\r\r\x182!\x1c!22222222222222222222222222222222222222222222222222\xff\xc0\x00\x11\x08\x01\x90\x01\x90\x03\x01"\x00\x02\x11\x01\x03\x11\x01\xff\xc4\x00\x1f\x00\x00\x01\x05\x01\x01\x01\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\xff\xc4\x00\xb5\x10\x00\x02\x01\x03\x03\x02\x04\x03\x05\x05\x04\x04\x00\x00\x01}\x01\x02\x03\x00\x04\x11\x05\x12!1A\x06\x13Qa\x07"q\x142\x81\x91\xa1\x08#B\xb1\xc1\x15R\xd1\xf0$3br\x82\t\n\x16\x17\x18\x19\x1a%&\'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz\x83\.................\xd9'
You can load a matrix with numpy:
pixels = np.frombuffer(r.content, dtype=np.uint8)
Then create a PIL Image with:
image = Image.fromarray(pixels 'RGB')
I think what solves your issue is changing this part of your code:
r = requests.get(url) # will only wait for one response and exit
you should replace with
r = requests.get(url, stream=True) # will stream data until finished
I think the issue is that as of right now your code exits prematurely before the whole imagedata was received. That's also why your object appears to be too small for a 400x400 image.
Cheers,
Dennis

How to show webcam capture in Plone site using OpenCV?

I am using Plone 4.3. I am trying to create a face recognition system on the Plone site. I need to show webcam captures using a template page. My sample code is below. However, when I run this code, I can't get the captured image in the template file.
sample.py:
class video(BrowserView):
video=ViewPageTemplateFile('video.pt')
def chow(self):
return self.video()
def show_video(self):
import cv2.cv as cv
cv.NamedWindow("camera", 1)
capture = cv.CaptureFromCAM(0)
while True:
img = cv.QueryFrame(capture)
return img
video.pt:
<div id="res">
<!--Here face is url name-->
<img id="draw" tal:attributes="src python:context.absolute_url() + '/face'">
<!--the above line refers to call a method of show_video()-->
</div>
This is a good starting point: http://plone.org/products/collective.takeaportrait (activate the optional OpenCV integration).
Keep in mind that there's a big difference between showing the camera output on the screen (that is simply HTML 5) and performing face recognition server side.

Categories

Resources