Last active
March 16, 2021 14:43
-
-
Save shikarunochi/5fee0a2f55db011978aa22ce66fee528 to your computer and use it in GitHub Desktop.
Image & Movie Sender for ESP32 ScreenShotReceiver
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ImageSender for ESP32 ScreenShotReceiver https://github.com/lovyan03/ESP32_ScreenShotReceiver | |
import sys | |
import socket | |
from PIL import Image | |
import io | |
IMAGE_QUALITY = 60 | |
LCD_WIDTH = 320 | |
LCD_HEIGHT = 320 | |
def sendImage(ipAddress, imageFile): | |
sourceImg = Image.open(imageFile) | |
# 幅、高さ | |
width,height = sourceImg.size | |
print("Original size:width:{}, height:{}".format(width, height)) | |
if height <= width: | |
re_width = int(width/height*LCD_WIDTH) | |
re_height = LCD_HEIGHT | |
else: | |
re_width = LCD_WIDTH | |
re_height = int(height/width*LCD_HEIGHT) | |
re_width = LCD_WIDTH | |
re_height = LCD_HEIGHT | |
sourceImg = sourceImg.resize((re_width, re_height), Image.LANCZOS) | |
img = Image.new(sourceImg.mode, (LCD_WIDTH, LCD_HEIGHT), (0,0,0)) | |
img.paste(sourceImg,(0,0)) | |
#https://qiita.com/ekzemplaro/items/6bd539983ba8997003b9 | |
output = io.BytesIO() | |
img.save(output, format='JPEG') | |
image_jpg = output.getvalue() | |
HEADER_SIZE = 4 | |
#TCP接続 | |
#https://qiita.com/__init__/items/5c89fa5b37b8c5ed32a4 | |
#https://qiita.com/tokoroten-lab/items/bb27351b393f087650a9 | |
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: | |
# サーバを指定 | |
s.connect((ipAddress, 63333)) | |
#packet_body = image_jpg.tostring() | |
packet_body = image_jpg | |
#prefix "JPG" 3Byte | |
packet_header = b'\x4a\x50\x47' + len(packet_body).to_bytes(HEADER_SIZE, byteorder='little',signed=False) | |
packet = packet_header + packet_body | |
# パケット送信 | |
try: | |
s.sendall(packet) | |
except socket.error as e: | |
print('Connection closed.') | |
# main | |
def main(): | |
args = sys.argv | |
if len(args) != 3: | |
print("usage: python movieSender.py [IP Address] [image Filename]") | |
quit() | |
sendImage(args[1],args[2]) | |
if __name__ == '__main__': | |
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#MovieSender for ESP32 ScreenShotReceiver https://github.com/lovyan03/ESP32_ScreenShotReceiver | |
import cv2 | |
import sys | |
import socket | |
IMAGE_QUALITY = 30 | |
LCD_WIDTH = 240 | |
LCD_HEIGHT = 200 | |
FRAME_SKIP = 10 | |
#https://www.souichi.club/deep-learning/spliter/ | |
def sendMovie(ipAddress, movie): | |
cap = cv2.VideoCapture(movie) | |
if not cap.isOpened(): | |
print("ファイルオープンできませんでした。" + movie) | |
return | |
# 幅 | |
width = cap.get(cv2.CAP_PROP_FRAME_WIDTH) | |
# 高さ | |
height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT) | |
# 総フレーム数 | |
count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) | |
# fps | |
fps = cap.get(cv2.CAP_PROP_FPS) | |
print("Original size:width:{}, height:{}, count:{}, fps:{}".format(width, height, count, fps)) | |
if height <= width: | |
re_width = int(width/height*LCD_WIDTH) | |
re_height = LCD_HEIGHT | |
start_pos_w = int((re_width -LCD_WIDTH)/2) | |
start_pos_h = 0 | |
else: | |
re_width = LCD_WIDTH | |
re_height = int(height/width*LCD_HEIGHT) | |
start_pos_w = 0 | |
start_pos_h = int((re_height -LCD_HEIGHT)/2) | |
# 指定されたサイズの配列を作成 | |
new_size = (re_width, re_height) | |
start_frame = 0 | |
HEADER_SIZE = 4 | |
#TCP接続 | |
#https://qiita.com/__init__/items/5c89fa5b37b8c5ed32a4 | |
#https://qiita.com/tokoroten-lab/items/bb27351b393f087650a9 | |
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: | |
# サーバを指定 | |
s.connect((ipAddress, 63333)) | |
for n in range(start_frame, count, FRAME_SKIP): | |
#while(cap.isOpened()): | |
cap.set(cv2.CAP_PROP_POS_FRAMES, n) | |
ret, frame_image = cap.read() # 画像を読み込む | |
# 指定されたサイズに変更する(縦横比は変更しない) | |
resize_image = cv2.resize(frame_image, new_size) | |
#JPEGデータとして送信 | |
(status, encoded_img) = cv2.imencode('.jpg', resize_image, [int(cv2.IMWRITE_JPEG_QUALITY), IMAGE_QUALITY]) | |
packet_body = encoded_img.tostring() | |
#prefix "JPG" 3Byte | |
packet_header = b'\x4a\x50\x47' + len(packet_body).to_bytes(HEADER_SIZE, byteorder='little',signed=False) | |
packet = packet_header + packet_body | |
# パケット送信 | |
try: | |
s.sendall(packet) | |
except socket.error as e: | |
print('Connection closed.') | |
break | |
# main | |
def main(): | |
args = sys.argv | |
if len(args) != 3: | |
print("usage: python movieSender.py [IP Address] [Movie Filename]") | |
quit() | |
sendMovie(args[1],args[2]) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment