程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Network picture transmission based on Python

編輯:Python

List of articles

    • server
    • client

Internet based image transmission is very common , The most common method is to compress the image into jpeg, Re pass socket Send to client , Let's go straight to the code .

  • socket
    • send
    • recv
  • imencode
  • imdecode

server

#!/usr/bin/env python3
#-*- coding: utf -*-
import socket
import cv2
s = socket.socket()
host = socket.gethostname()
port = 12345
s.bind((host, port))
camera = cv2.VideoCapture(0)
if camera.isOpened():
success, frame = camera.read()
s.listen(5)
c, addr = s.accept()
while True:
success, frame = camera.read()
result, img_code = cv2.imencode('.jpg', frame)
buffer = frame.tobytes()
c.send(buffer)
c.close() # Close the connection 

client

#!/usr/bin/env python3
#-*- coding: utf -*-
import socket
import cv2
import numpy as np
s = socket.socket()
host = socket.gethostname()
port = 12345
s.connect((host, port))
while True:
data = s.recv(921600)
buffer = np.frombuffer(data, np.uint8)
frame = buffer.reshape(480, 640, 3)
frame2 = cv2.flip(frame, 1)
cv2.imshow('image', frame2)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cv2.destroyAllWindows()
s.close()

–end


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved