gpt4 book ai didi

android - 解码错误,图像格式不受 Azure 支持

转载 作者:行者123 更新时间:2023-11-29 23:30:53 28 4
gpt4 key购买 nike

我在后端运行一个 flask 服务器。我想从移动应用程序读取图像并将其发送到服务器以检测人脸。

这是用于以字节形式发送图像的 java 代码(客户端)-

public class client {
public static void main(String [] args) throws Exception{

String url = "http://127.0.0.1:8080/facial";
// 2. create obj for the URL class
URL obj = new URL(url);
// 3. open connection on the url
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type","image/jpeg");
con.setDoInput(true);
con.setDoOutput(true);


try {

System.out.println("Reading image from disk. ");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
baos.flush();
File file = new File("jpeg.jpg");
BufferedImage image1 = ImageIO.read(file);

ImageIO.write(image1, "jpg", baos);
baos.flush();
System.out.println(baos.size());
byte[] bytes = baos.toByteArray();
baos.close();

System.out.println("Sending image to server. ");

OutputStream out = con.getOutputStream();
DataOutputStream image = new DataOutputStream(out);

image.writeInt(bytes.length);
image.write(bytes, 0, bytes.length);

System.out.println("Image sent to server. ");

image.close();
// close the output stream
out.close();

}catch (Exception e) {
System.out.println("Exception: " + e.getMessage());
}

// define object for the reply from the server
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
//Get response from server
int responseCode = con.getResponseCode();
System.out.println("Response Code : " + responseCode);
// read in the response from the server
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
System.out.println(inputLine);
}
// close the input stream
in.close();

}

}

这是我的服务器代码 -

def get_facial(data):
face_api_url = 'https://southeastasia.api.cognitive.microsoft.com/face/v1.0/detect'

# Set image_url to the URL of an image that you want to analyze.
headers = {'Ocp-Apim-Subscription-Key': subscription_key,
"Content-Type":"application/octet-stream"
}
params = {
'returnFaceId': 'true',
'returnFaceLandmarks': 'false',
'returnFaceAttributes': 'age,gender,headPose,smile,facialHair,glasses,' +
'emotion,hair,makeup,occlusion,accessories,blur,exposure,noise'
}
response = requests.post(face_api_url, params=params, headers=headers, data=data)
faces = response.json()
res={}
import pdb; pdb.set_trace()

res["status"] = '200'
res["num"] = str(len(faces))
return res

@app.route('/facial',methods=['POST'])
def facial():
import pdb; pdb.set_trace()
data=bytes(request.get_data())
res={}
try:
res = get_facial(data)
except:
res['status'] = '404'
print(res)
return json.dumps(res)

检查后 - 我从另一个 python 文件 发送了相同的图像 并检查了数据的大小。它是 102564 字节并且它有效但是从 java 代码读取和发送的相同图像是 106208 字节。我不知道错误到底在哪里。感谢任何帮助!!:-)

最佳答案

我找到了解决这个问题的快速方法 -

Path path = Paths.get("jpeg.jpg");
byte[] fileContents = Files.readAllBytes(path);
image.write(fileContents, 0, fileContents.length);

我不完全知道为什么从 imageio 读取失败。我的猜测是它也读取 jpg 文件的文件头。

关于android - 解码错误,图像格式不受 Azure 支持,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52689777/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com