You want to create a socket communication that sends a picture to the server as soon as you take a picture with a button in Raspberry.
I'm still a beginner, so I wrote the following code with the information I got from searching here and there.
Photography and camera shutdown work, but the transfer is blocked immediately after taking the picture.
So I keep changing the call position of the transfer function, but it hasn't been solved, so I'm asking you a question
The code below is the client code, and it says that the picture needed for the transfer at runtime is not stored in the directory, but I don't know how to change the order.
I would appreciate it if you could point out other problems in the code.
# TCP client
#-*-coding: utf-8-*-
#-*-coding: euc-kr-*-
import socket
import sys
import os
import time
import picamera
import RPi.GPIO as GPIO
import datetime
GPIO.setmode(GPIO.BCM)
GPIO.setup(21, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(20, GPIO.IN, pull_up_down=GPIO.PUD_UP)
def fileName():
dte = time.localtime()
Year = dte.tm_year
Mon = dte.tm_mon
Day = dte.tm_mday
WDay = dte.tm_wday
Hour = dte.tm_hour
Min = dte.tm_min
Sec = dte.tm_sec
imgFileName = str(Year) + '_' + str(Mon) + '_' + str(Day) + '_' + str(Hour) + '_' + str(Min) + '_' + str(
Sec) + '.jpg';
return imgFileName
print("Start Camera App")
# # Server File Path
src = "C:\\Users\\HJ_2\\Desktop\\programming\\save\\";
def transfer(filename):
capture_file_name = "/home/pi/Desktop/picture/" + str(saveFileName) + ".jpg"
# # 3 Send File
# # Import img (path/name)
file = open(capture_file_name, "rb")
img_size = os.path.getsize(capture_file_name)
img = file.read(img_size) # saved image
file.close()
# # connect server
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect(("192.168.0.6", 5001))
# # image tramsfer
client_socket.sendall(img)
# # Terminate connection with server
client_socket.close()
print("Finish SendAll")
with picamera.PiCamera() as camera:
camera.resolution = (640,480)
camera.rotation = 0
camera.framerate = 24
camera.start_preview(fullscreen=False, window=(150,50,640,480))
frame = 1
while True:
GPIO.wait_for_edge(21, GPIO.FALLING)
saveFileName = fileName()
camera.capture('/home/pi/Desktop/picture/' + saveFileName + '_%03d.jpg' %frame)
fileName_list = [saveFileName]
if saveFileName in fileName_list:
transfer(saveFileName)
time.sleep(3)
frame += 1
while GPIO.input(20):
camera.annotate_text = ''
print("App Stop")
camera.stop_preview()
camera.close()
GPIO.cleanup()
When executing the code above, the message below appears.
Traceback (most recent call last):
File "last_2.py", line 76, in <module>
transfer(saveFileName)
File "last_2.py", line 48, in transfer
file = open(capture_file_name, "rb")
IOError: [Errno 2] No such file or directory: '/home/pi/Desktop/picture/2019_10_29_17_6_44.jpg.jpg
Read the error message carefully.
No such file or directory: '/home/pi/Desktop/picture/2019_10_29_17_6_44.jpg.jpg
It says there are no files or directories named above.
This file name is
def transfer(filename):
capture_file_name = "/home/pi/Desktop/picture/" + str(saveFileName) + ".jpg"
# # 3 Send File
# # Import img (path/name)
file = open(capture_file_name, "rb")
This is the capture_file_name of the above code. This file name is different from the saved file name.
The code for saving is
camera.capture('/home/pi/Desktop/picture/' + saveFileName + '_%03d.jpg' %frame)
There seems to be a difference.
There seems to be a lot of other problems, but if I just tell you what you see at the top,
#-*-coding: utf-8-*-
#-*-coding: euc-kr-*-
The above means that the source file is a file encoded with utf-8 character set encoding. The second is that the source file is encoded in euc-kr. Neither of them is valid. In Python 3 version, you don't need to lose it, just save the sauce to utf-8. During the text transmission process, I think I saw the wrong blog post somewhere and put it in because of the encoding problem, but it doesn't matter. When transmitting, the character must be specified with the desired encoding and explicitly encoded with s.code('utf-8')
to obtain a binary before being thrown.
932 When building Fast API+Uvicorn environment with PyInstaller, console=False results in an error
576 rails db:create error: Could not find mysql2-0.5.4 in any of the sources
579 Who developed the "avformat-59.dll" that comes with FFmpeg?
626 GDB gets version error when attempting to debug with the Presense SDK (IDE)
© 2024 OneMinuteCode. All rights reserved.