1. Download Mobile Software
You need to download the IP camera software on the mobile phone. You can turn the mobile device into a wireless IP camera with two-way audio support through the built-in RTSP and HTTP server and use it for security monitoring. You can use the browser on the computer to view it. Here I use OpenCV to read the video stream and record the screen
2. Get video stream address
Open the next good software and click "open IP camera server" to obtain the address of the video stream under the current intranet. Here, I choose the first video stream of RTSP protocol. When accessing, I need to enter the account password, which is admin by default
!
3. Code writing
3.1 reading video stream
Create a new VideoCapture object and read the video stream
cap = cv2.VideoCapture("rtsp://admin:admin@******.local:8554/live")
3.2 display video
Use the while statement to cycle through the video and display it
while True: success,img = cap.read() cv2.imshow("camera",img)
3.3 recording video
To create a VideoWriter object and write an image into a video, you need to specify several parameters
- Represents the new file stored in the read video frame
- It refers to the coding format of video storage
- Represents the number of frames per second
- Represents the length, width and size of the image
fourcc = cv2.VideoWriter_fourcc(*'mp4v') size = (int(self.capture.get(cv2.CAP_PROP_FRAME_WIDTH)), \ int(self.capture.get(cv2.CAP_PROP_FRAME_HEIGHT))) out = cv2.VideoWriter("name.mp4", fourcc, 25, self.size) out.write(img) #Write pictures to video
4. Effect display
5. Code display
According to the above ideas, I have encapsulated and optimized the code. By using the space bar to start and end the recording, press ESC to exit the program, and the current recording state and current time are displayed in the video. Normal indicates the normal display state, Recoding indicates that it is recording, and the space bar can be used to switch the state. If you use ESC to launch the program without pressing the pause key, you can also save the video normally.
import cv2 import time class Camera: def __init__(self, addr, save_dir="./"): """initial value""" self.save_dir = save_dir self.addr = addr self.isRecoding = False self.capture =cv2.VideoCapture(self.addr) self.fourcc = cv2.VideoWriter_fourcc(*'mp4v') self.size = (int(self.capture.get(cv2.CAP_PROP_FRAME_WIDTH)), \ int(self.capture.get(cv2.CAP_PROP_FRAME_HEIGHT))) def exit(self): """exit""" self.capture.release() if self.isRecoding: self.out.release() cv2.destroyWindow("camera") print("exit") def mark(self,img): now = int(time.time()) timeArray = time.localtime(now) if self.isRecoding: state = "Recoding:" else: state = "Normal:" otherStyleTime = state + time.strftime("%Y-%m-%d %H:%M:%S", timeArray) otherStyleTime = otherStyleTime.encode("gbk").decode(errors="ignore") cv2.putText(img, otherStyleTime, (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (55,255,155), 2) return img def open(self): """open camera""" cv2.namedWindow("camera",1) while (self.capture.isOpened()): success,img = self.capture.read() if success: cv2.namedWindow("camera",0) key = cv2.waitKey(5) if key == 32: #Press the spacebar to start and end recording self.isRecoding = not self.isRecoding print("isRecoding",self.isRecoding) if self.isRecoding: vedeo_name = time.strftime("%Y%m%d%H%M%S", time.localtime(int(time.time()))) + ".mp4" self.out = cv2.VideoWriter(self.save_dir+vedeo_name, self.fourcc, 25, self.size) print() else: self.out.release() img = self.mark(img) result = cv2.cvtColor(img, cv2.COLOR_RGB2BGR) if self.isRecoding: self.out.write(result) cv2.imshow("camera",img) if key == 27: #ESC key exit self.exit() else: print("Fail to get image") if __name__ == '__main__': addr = "rtsp://admin:admin@zzttekiiPhone.local:8554/live" cam1 = Camera(addr) cv2.namedWindow("camera",1) cam1.open()
6. Download address
The file has been uploaded to Gitee and can be downloaded if necessary
https://gitee.com/ZT-Brilly/practice/tree/master/Camera