A simple implementation of data integration between Python and Java

1, Images detected by Python can be displayed on the platform

Development environment:
Computer 1: Python implementation of human body detection and real-time saving of images with abnormal movements,
Computer 2: Java Implementation Platform Development.

Problem description:
When the computer detects an abnormal image, it stores the abnormal image and other information in the database, and the front-end page of the platform can display the local image of computer one normally.

Implementation ideas: .

  1. Python saves detected abnormal images in real-time
if task.action_preds:
time_now = datetime.datetime.now().strftime('%Y%m%d%H%M%S')
str_img_name = str(time_now) + ".jpg"
save_img_path = os.path.join(abnor_path, str_img_name)
# abnor_path path of abnormal image'D:/work_python/flask-video-web/abnor_picture/'
for p in task.action_preds:
  for label in p:
      if label[0] == 'fall down':
          for ii in task.frames:
              behavior = '3'  # 'fall'
              ab_path = save_img_path
              cv2.imwrite(save_img_path, ii)  #save abnormal images 
  1. Python passes images to the Java interface, and then stores the images and other information in the database
if behavior and ab_path:
# 1.python call Java interface to transfer images to Java
url = 'http://127.0.0.1:8080/upload'	#change to Java written interface 
files = {"code": "200", 'file': (str_img_name,open(ab_path, 'rb'), 'image/jpeg')}
r = requests.post(url, files=files)
# 2.Java return the name of the image it received 
resp = json.dumps(r.text)
# 3.python store received images and other information in the database 
sql1 = 'INSERT INTO avatar(type,avatar,ip_id,start_time) VALUES(%s,%s,%s,%s)'	#database SQL statement 
curosr = conn()
curosr.execute(sql1, (behavior, resp, ip, current_time))

Reference link: Simple implementation of image transfer in Python and Java HTTP
I took many detours in writing interfaces before, and I am very grateful for the author's article.

2, Python receives data from Java

Problem description:
The client has operations to add or delete data, and Java needs to pass the added or deleted data to Python.

Implementation ideas: .

  1. Python server
import socket
adress = ('', 4000)   # Java the interface accessed is python computer IP
#establish socket object, while setting communication mode, AF_INET representative IPv4,SOCK_STREAM representative flow type socket , using tcp protocol 
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(address)
#start listening, with a maximum of 5 pending connections 
server.listen(5)
#infinite loop, achieving repeated request reception 
while True:
	# accept the () method passively accepts client connections, blocks, and waits for connections . client it's for the client socket object that can receive and send messages, addr indicates the address of the client 
	client, addr = server.accept()
	data = client.recv(13)  #represent reading 13 from the sent data byte data for 
	data1 = client.recv(4)   #re receive 4 byte data 
	client.sendall(b'1')    #to reply to the client, it must be byte type 
	client.close()        #close connection 
server.close()
  1. Java client

Code reference link: Network Programming Part - Socket Implementation for Python and Java Communication
Attention!!! Java developers also need to use sockets for docking.