|
|
@@ -545,335 +545,6 @@ print(f"Output image saved at {output_image_path}")
|
|
|
print("\nDetected objects:")
|
|
|
print(result["detectedObjects"])
|
|
|
</code></pre></details>
|
|
|
-
|
|
|
-<details><summary>C++</summary>
|
|
|
-
|
|
|
-<pre><code class="language-cpp">#include <iostream>
|
|
|
-#include "cpp-httplib/httplib.h" // https://github.com/Huiyicc/cpp-httplib
|
|
|
-#include "nlohmann/json.hpp" // https://github.com/nlohmann/json
|
|
|
-#include "base64.hpp" // https://github.com/tobiaslocker/base64
|
|
|
-
|
|
|
-int main() {
|
|
|
- httplib::Client client("localhost:8080");
|
|
|
- const std::string imagePath = "./demo.jpg";
|
|
|
- const std::string outputImagePath = "./out.jpg";
|
|
|
-
|
|
|
- httplib::Headers headers = {
|
|
|
- {"Content-Type", "application/json"}
|
|
|
- };
|
|
|
-
|
|
|
- // 对本地图像进行Base64编码
|
|
|
- std::ifstream file(imagePath, std::ios::binary | std::ios::ate);
|
|
|
- std::streamsize size = file.tellg();
|
|
|
- file.seekg(0, std::ios::beg);
|
|
|
-
|
|
|
- std::vector<char> buffer(size);
|
|
|
- if (!file.read(buffer.data(), size)) {
|
|
|
- std::cerr << "Error reading file." << std::endl;
|
|
|
- return 1;
|
|
|
- }
|
|
|
- std::string bufferStr(reinterpret_cast<const char*>(buffer.data()), buffer.size());
|
|
|
- std::string encodedImage = base64::to_base64(bufferStr);
|
|
|
-
|
|
|
- nlohmann::json jsonObj;
|
|
|
- jsonObj["image"] = encodedImage;
|
|
|
- std::string body = jsonObj.dump();
|
|
|
-
|
|
|
- // 调用API
|
|
|
- auto response = client.Post("/small-object-detection", headers, body, "application/json");
|
|
|
- // 处理接口返回数据
|
|
|
- if (response && response->status == 200) {
|
|
|
- nlohmann::json jsonResponse = nlohmann::json::parse(response->body);
|
|
|
- auto result = jsonResponse["result"];
|
|
|
-
|
|
|
- encodedImage = result["image"];
|
|
|
- std::string decodedString = base64::from_base64(encodedImage);
|
|
|
- std::vector<unsigned char> decodedImage(decodedString.begin(), decodedString.end());
|
|
|
- std::ofstream outputImage(outPutImagePath, std::ios::binary | std::ios::out);
|
|
|
- if (outputImage.is_open()) {
|
|
|
- outputImage.write(reinterpret_cast<char*>(decodedImage.data()), decodedImage.size());
|
|
|
- outputImage.close();
|
|
|
- std::cout << "Output image saved at " << outPutImagePath << std::endl;
|
|
|
- } else {
|
|
|
- std::cerr << "Unable to open file for writing: " << outPutImagePath << std::endl;
|
|
|
- }
|
|
|
-
|
|
|
- auto detectedObjects = result["detectedObjects"];
|
|
|
- std::cout << "\nDetected objects:" << std::endl;
|
|
|
- for (const auto& category : detectedObjects) {
|
|
|
- std::cout << category << std::endl;
|
|
|
- }
|
|
|
- } else {
|
|
|
- std::cout << "Failed to send HTTP request." << std::endl;
|
|
|
- return 1;
|
|
|
- }
|
|
|
-
|
|
|
- return 0;
|
|
|
-}
|
|
|
-</code></pre></details>
|
|
|
-
|
|
|
-<details><summary>Java</summary>
|
|
|
-
|
|
|
-<pre><code class="language-java">import okhttp3.*;
|
|
|
-import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
-import com.fasterxml.jackson.databind.JsonNode;
|
|
|
-import com.fasterxml.jackson.databind.node.ObjectNode;
|
|
|
-
|
|
|
-import java.io.File;
|
|
|
-import java.io.FileOutputStream;
|
|
|
-import java.io.IOException;
|
|
|
-import java.util.Base64;
|
|
|
-
|
|
|
-public class Main {
|
|
|
- public static void main(String[] args) throws IOException {
|
|
|
- String API_URL = "http://localhost:8080/small-object-detection"; // 服务URL
|
|
|
- String imagePath = "./demo.jpg"; // 本地图像
|
|
|
- String outputImagePath = "./out.jpg"; // 输出图像
|
|
|
-
|
|
|
- // 对本地图像进行Base64编码
|
|
|
- File file = new File(imagePath);
|
|
|
- byte[] fileContent = java.nio.file.Files.readAllBytes(file.toPath());
|
|
|
- String imageData = Base64.getEncoder().encodeToString(fileContent);
|
|
|
-
|
|
|
- ObjectMapper objectMapper = new ObjectMapper();
|
|
|
- ObjectNode params = objectMapper.createObjectNode();
|
|
|
- params.put("image", imageData); // Base64编码的文件内容或者图像URL
|
|
|
-
|
|
|
- // 创建 OkHttpClient 实例
|
|
|
- OkHttpClient client = new OkHttpClient();
|
|
|
- MediaType JSON = MediaType.Companion.get("application/json; charset=utf-8");
|
|
|
- RequestBody body = RequestBody.Companion.create(params.toString(), JSON);
|
|
|
- Request request = new Request.Builder()
|
|
|
- .url(API_URL)
|
|
|
- .post(body)
|
|
|
- .build();
|
|
|
-
|
|
|
- // 调用API并处理接口返回数据
|
|
|
- try (Response response = client.newCall(request).execute()) {
|
|
|
- if (response.isSuccessful()) {
|
|
|
- String responseBody = response.body().string();
|
|
|
- JsonNode resultNode = objectMapper.readTree(responseBody);
|
|
|
- JsonNode result = resultNode.get("result");
|
|
|
- String base64Image = result.get("image").asText();
|
|
|
- JsonNode detectedObjects = result.get("detectedObjects");
|
|
|
-
|
|
|
- byte[] imageBytes = Base64.getDecoder().decode(base64Image);
|
|
|
- try (FileOutputStream fos = new FileOutputStream(outputImagePath)) {
|
|
|
- fos.write(imageBytes);
|
|
|
- }
|
|
|
- System.out.println("Output image saved at " + outputImagePath);
|
|
|
- System.out.println("\nDetected objects: " + detectedObjects.toString());
|
|
|
- } else {
|
|
|
- System.err.println("Request failed with code: " + response.code());
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-}
|
|
|
-</code></pre></details>
|
|
|
-
|
|
|
-<details><summary>Go</summary>
|
|
|
-
|
|
|
-<pre><code class="language-go">package main
|
|
|
-
|
|
|
-import (
|
|
|
- "bytes"
|
|
|
- "encoding/base64"
|
|
|
- "encoding/json"
|
|
|
- "fmt"
|
|
|
- "io/ioutil"
|
|
|
- "net/http"
|
|
|
-)
|
|
|
-
|
|
|
-func main() {
|
|
|
- API_URL := "http://localhost:8080/small-object-detection"
|
|
|
- imagePath := "./demo.jpg"
|
|
|
- outputImagePath := "./out.jpg"
|
|
|
-
|
|
|
- // 对本地图像进行Base64编码
|
|
|
- imageBytes, err := ioutil.ReadFile(imagePath)
|
|
|
- if err != nil {
|
|
|
- fmt.Println("Error reading image file:", err)
|
|
|
- return
|
|
|
- }
|
|
|
- imageData := base64.StdEncoding.EncodeToString(imageBytes)
|
|
|
-
|
|
|
- payload := map[string]string{"image": imageData} // Base64编码的文件内容或者图像URL
|
|
|
- payloadBytes, err := json.Marshal(payload)
|
|
|
- if err != nil {
|
|
|
- fmt.Println("Error marshaling payload:", err)
|
|
|
- return
|
|
|
- }
|
|
|
-
|
|
|
- // 调用API
|
|
|
- client := &http.Client{}
|
|
|
- req, err := http.NewRequest("POST", API_URL, bytes.NewBuffer(payloadBytes))
|
|
|
- if err != nil {
|
|
|
- fmt.Println("Error creating request:", err)
|
|
|
- return
|
|
|
- }
|
|
|
-
|
|
|
- res, err := client.Do(req)
|
|
|
- if err != nil {
|
|
|
- fmt.Println("Error sending request:", err)
|
|
|
- return
|
|
|
- }
|
|
|
- defer res.Body.Close()
|
|
|
-
|
|
|
- // 处理接口返回数据
|
|
|
- body, err := ioutil.ReadAll(res.Body)
|
|
|
- if err != nil {
|
|
|
- fmt.Println("Error reading response body:", err)
|
|
|
- return
|
|
|
- }
|
|
|
- type Response struct {
|
|
|
- Result struct {
|
|
|
- Image string `json:"image"`
|
|
|
- DetectedObjects []map[string]interface{} `json:"detectedObjects"`
|
|
|
- } `json:"result"`
|
|
|
- }
|
|
|
- var respData Response
|
|
|
- err = json.Unmarshal([]byte(string(body)), &respData)
|
|
|
- if err != nil {
|
|
|
- fmt.Println("Error unmarshaling response body:", err)
|
|
|
- return
|
|
|
- }
|
|
|
-
|
|
|
- outputImageData, err := base64.StdEncoding.DecodeString(respData.Result.Image)
|
|
|
- if err != nil {
|
|
|
- fmt.Println("Error decoding base64 image data:", err)
|
|
|
- return
|
|
|
- }
|
|
|
- err = ioutil.WriteFile(outputImagePath, outputImageData, 0644)
|
|
|
- if err != nil {
|
|
|
- fmt.Println("Error writing image to file:", err)
|
|
|
- return
|
|
|
- }
|
|
|
- fmt.Printf("Image saved at %s.jpg\n", outputImagePath)
|
|
|
- fmt.Println("\nDetected objects:")
|
|
|
- for _, category := range respData.Result.DetectedObjects {
|
|
|
- fmt.Println(category)
|
|
|
- }
|
|
|
-}
|
|
|
-</code></pre></details>
|
|
|
-
|
|
|
-<details><summary>C#</summary>
|
|
|
-
|
|
|
-<pre><code class="language-csharp">using System;
|
|
|
-using System.IO;
|
|
|
-using System.Net.Http;
|
|
|
-using System.Net.Http.Headers;
|
|
|
-using System.Text;
|
|
|
-using System.Threading.Tasks;
|
|
|
-using Newtonsoft.Json.Linq;
|
|
|
-
|
|
|
-class Program
|
|
|
-{
|
|
|
- static readonly string API_URL = "http://localhost:8080/small-object-detection";
|
|
|
- static readonly string imagePath = "./demo.jpg";
|
|
|
- static readonly string outputImagePath = "./out.jpg";
|
|
|
-
|
|
|
- static async Task Main(string[] args)
|
|
|
- {
|
|
|
- var httpClient = new HttpClient();
|
|
|
-
|
|
|
- // 对本地图像进行Base64编码
|
|
|
- byte[] imageBytes = File.ReadAllBytes(imagePath);
|
|
|
- string image_data = Convert.ToBase64String(imageBytes);
|
|
|
-
|
|
|
- var payload = new JObject{ { "image", image_data } }; // Base64编码的文件内容或者图像URL
|
|
|
- var content = new StringContent(payload.ToString(), Encoding.UTF8, "application/json");
|
|
|
-
|
|
|
- // 调用API
|
|
|
- HttpResponseMessage response = await httpClient.PostAsync(API_URL, content);
|
|
|
- response.EnsureSuccessStatusCode();
|
|
|
-
|
|
|
- // 处理接口返回数据
|
|
|
- string responseBody = await response.Content.ReadAsStringAsync();
|
|
|
- JObject jsonResponse = JObject.Parse(responseBody);
|
|
|
-
|
|
|
- string base64Image = jsonResponse["result"]["image"].ToString();
|
|
|
- byte[] outputImageBytes = Convert.FromBase64String(base64Image);
|
|
|
-
|
|
|
- File.WriteAllBytes(outputImagePath, outputImageBytes);
|
|
|
- Console.WriteLine($"Output image saved at {outputImagePath}");
|
|
|
- Console.WriteLine("\nDetected objects:");
|
|
|
- Console.WriteLine(jsonResponse["result"]["detectedObjects"].ToString());
|
|
|
- }
|
|
|
-}
|
|
|
-</code></pre></details>
|
|
|
-
|
|
|
-<details><summary>Node.js</summary>
|
|
|
-
|
|
|
-<pre><code class="language-js">const axios = require('axios');
|
|
|
-const fs = require('fs');
|
|
|
-
|
|
|
-const API_URL = 'http://localhost:8080/small-object-detection'
|
|
|
-const imagePath = './demo.jpg'
|
|
|
-const outputImagePath = "./out.jpg";
|
|
|
-
|
|
|
-let config = {
|
|
|
- method: 'POST',
|
|
|
- maxBodyLength: Infinity,
|
|
|
- url: API_URL,
|
|
|
- data: JSON.stringify({
|
|
|
- 'image': encodeImageToBase64(imagePath) // Base64编码的文件内容或者图像URL
|
|
|
- })
|
|
|
-};
|
|
|
-
|
|
|
-// 对本地图像进行Base64编码
|
|
|
-function encodeImageToBase64(filePath) {
|
|
|
- const bitmap = fs.readFileSync(filePath);
|
|
|
- return Buffer.from(bitmap).toString('base64');
|
|
|
-}
|
|
|
-
|
|
|
-// 调用API
|
|
|
-axios.request(config)
|
|
|
-.then((response) => {
|
|
|
- // 处理接口返回数据
|
|
|
- const result = response.data["result"];
|
|
|
- const imageBuffer = Buffer.from(result["image"], 'base64');
|
|
|
- fs.writeFile(outputImagePath, imageBuffer, (err) => {
|
|
|
- if (err) throw err;
|
|
|
- console.log(`Output image saved at ${outputImagePath}`);
|
|
|
- });
|
|
|
- console.log("\nDetected objects:");
|
|
|
- console.log(result["detectedObjects"]);
|
|
|
-})
|
|
|
-.catch((error) => {
|
|
|
- console.log(error);
|
|
|
-});
|
|
|
-</code></pre></details>
|
|
|
-
|
|
|
-<details><summary>PHP</summary>
|
|
|
-
|
|
|
-<pre><code class="language-php"><?php
|
|
|
-
|
|
|
-$API_URL = "http://localhost:8080/small-object-detection"; // 服务URL
|
|
|
-$image_path = "./demo.jpg";
|
|
|
-$output_image_path = "./out.jpg";
|
|
|
-
|
|
|
-// 对本地图像进行Base64编码
|
|
|
-$image_data = base64_encode(file_get_contents($image_path));
|
|
|
-$payload = array("image" => $image_data); // Base64编码的文件内容或者图像URL
|
|
|
-
|
|
|
-// 调用API
|
|
|
-$ch = curl_init($API_URL);
|
|
|
-curl_setopt($ch, CURLOPT_POST, true);
|
|
|
-curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload));
|
|
|
-curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
|
|
|
-curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
|
-$response = curl_exec($ch);
|
|
|
-curl_close($ch);
|
|
|
-
|
|
|
-// 处理接口返回数据
|
|
|
-$result = json_decode($response, true)["result"];
|
|
|
-file_put_contents($output_image_path, base64_decode($result["image"]));
|
|
|
-echo "Output image saved at " . $output_image_path . "\n";
|
|
|
-echo "\nDetected objects:\n";
|
|
|
-print_r($result["detectedObjects"]);
|
|
|
-
|
|
|
-?>
|
|
|
-</code></pre></details>
|
|
|
</details>
|
|
|
<br/>
|
|
|
|