Skip to content

Commit

Permalink
Fix fastapi app bugs (#2527)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bobholamovic authored Nov 21, 2024
1 parent 0a408a3 commit 415ce5c
Show file tree
Hide file tree
Showing 7 changed files with 292 additions and 157 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -411,9 +411,14 @@ Below are the API references and multi-language service invocation examples:
<td>Positions and contents of formulas.</td>
</tr>
<tr>
<td><code>image</code></td>
<td><code>layoutImage</code></td>
<td><code>string</code></td>
<td>Formula recognition result image with detected formula positions annotated. The image is in JPEG format and encoded in Base64.</td>
<td>Layout area detection result image. The image is in JPEG format and encoded using Base64.</td>
</tr>
<tr>
<td><code>ocrImage</code></td>
<td><code>string</code></td>
<td>OCR result image. The image is in JPEG format and encoded using Base64.</td>
</tr>
</tbody>
</table>
Expand Down Expand Up @@ -464,7 +469,8 @@ Below are the API references and multi-language service invocation examples:
&quot;latex&quot;: &quot;F({\bf x})=C(F_{1}(x_{1}),\cdot\cdot\cdot,F_{N}(x_{N})).\qquad\qquad\qquad(1)&quot;
}
],
&quot;image&quot;: &quot;xxxxxx&quot;
&quot;layoutImage&quot;: &quot;xxxxxx&quot;,
&quot;ocrImage&quot;: &quot;xxxxxx&quot;
}
</code></pre></details>

Expand All @@ -479,7 +485,7 @@ import requests

API_URL = &quot;http://localhost:8080/formula-recognition&quot;
image_path = &quot;./demo.jpg&quot;
output_image_path = &quot;./out.jpg&quot;
layout_image_path = &quot;./layout.jpg&quot;

with open(image_path, &quot;rb&quot;) as file:
image_bytes = file.read()
Expand All @@ -491,9 +497,9 @@ response = requests.post(API_URL, json=payload)

assert response.status_code == 200
result = response.json()[&quot;result&quot;]
with open(output_image_path, &quot;wb&quot;) as file:
file.write(base64.b64decode(result[&quot;image&quot;]))
print(f&quot;Output image saved at {output_image_path}&quot;)
with open(layout_image_path, &quot;wb&quot;) as file:
file.write(base64.b64decode(result[&quot;layoutImage&quot;]))
print(f&quot;Output image saved at {layout_image_path}&quot;)
print(&quot;\nDetected formulas:&quot;)
print(result[&quot;formulas&quot;])
</code></pre></details>
Expand All @@ -508,7 +514,7 @@ print(result[&quot;formulas&quot;])
int main() {
httplib::Client client(&quot;localhost:8080&quot;);
const std::string imagePath = &quot;./demo.jpg&quot;;
const std::string outputImagePath = &quot;./out.jpg&quot;;
const std::string layoutImagePath = &quot;./layout.jpg&quot;;

httplib::Headers headers = {
{&quot;Content-Type&quot;, &quot;application/json&quot;}
Expand All @@ -535,16 +541,16 @@ int main() {
nlohmann::json jsonResponse = nlohmann::json::parse(response-&gt;body);
auto result = jsonResponse[&quot;result&quot;];

encodedImage = result[&quot;image&quot;];
std::string decodedString = base64::from_base64(encodedImage);
std::vector&lt;unsigned char&gt; decodedImage(decodedString.begin(), decodedString.end());
std::ofstream outputImage(outPutImagePath, std::ios::binary | std::ios::out);
if (outputImage.is_open()) {
outputImage.write(reinterpret_cast&lt;char*&gt;(decodedImage.data()), decodedImage.size());
outputImage.close();
std::cout &lt;&lt; &quot;Output image saved at &quot; &lt;&lt; outPutImagePath &lt;&lt; std::endl;
encodedImage = result[&quot;layoutImage&quot;];
decodedString = base64::from_base64(encodedImage);
std::vector&lt;unsigned char&gt; decodedLayoutImage(decodedString.begin(), decodedString.end());
std::ofstream outputLayoutFile(layoutImagePath, std::ios::binary | std::ios::out);
if (outputLayoutFile.is_open()) {
outputLayoutFile.write(reinterpret_cast&lt;char*&gt;(decodedLayoutImage.data()), decodedLayoutImage.size());
outputLayoutFile.close();
std::cout &lt;&lt; &quot;Output image saved at &quot; &lt;&lt; layoutImagePath &lt;&lt; std::endl;
} else {
std::cerr &lt;&lt; &quot;Unable to open file for writing: &quot; &lt;&lt; outPutImagePath &lt;&lt; std::endl;
std::cerr &lt;&lt; &quot;Unable to open file for writing: &quot; &lt;&lt; layoutImagePath &lt;&lt; std::endl;
}

auto formulas = result[&quot;formulas&quot;];
Expand Down Expand Up @@ -577,7 +583,7 @@ public class Main {
public static void main(String[] args) throws IOException {
String API_URL = &quot;http://localhost:8080/formula-recognition&quot;;
String imagePath = &quot;./demo.jpg&quot;;
String outputImagePath = &quot;./out.jpg&quot;;
String layoutImagePath = &quot;./layout.jpg&quot;;

File file = new File(imagePath);
byte[] fileContent = java.nio.file.Files.readAllBytes(file.toPath());
Expand All @@ -600,14 +606,15 @@ public class Main {
String responseBody = response.body().string();
JsonNode resultNode = objectMapper.readTree(responseBody);
JsonNode result = resultNode.get(&quot;result&quot;);
String base64Image = result.get(&quot;image&quot;).asText();
String layoutBase64Image = result.get(&quot;layoutImage&quot;).asText();
JsonNode formulas = result.get(&quot;formulas&quot;);

byte[] imageBytes = Base64.getDecoder().decode(base64Image);
try (FileOutputStream fos = new FileOutputStream(outputImagePath)) {
imageBytes = Base64.getDecoder().decode(layoutBase64Image);
try (FileOutputStream fos = new FileOutputStream(layoutImagePath)) {
fos.write(imageBytes);
}
System.out.println(&quot;Output image saved at &quot; + outputImagePath);
System.out.println(&quot;Output image saved at &quot; + layoutImagePath);

System.out.println(&quot;\nDetected formulas: &quot; + formulas.toString());
} else {
System.err.println(&quot;Request failed with code: &quot; + response.code());
Expand All @@ -633,7 +640,7 @@ import (
func main() {
API_URL := &quot;http://localhost:8080/formula-recognition&quot;
imagePath := &quot;./demo.jpg&quot;
outputImagePath := &quot;./out.jpg&quot;
layoutImagePath := &quot;./layout.jpg&quot;

imageBytes, err := ioutil.ReadFile(imagePath)
if err != nil {
Expand Down Expand Up @@ -670,7 +677,7 @@ func main() {
}
type Response struct {
Result struct {
Image string `json:&quot;image&quot;`
LayoutImage string `json:&quot;layoutImage&quot;`
Formulas []map[string]interface{} `json:&quot;formulas&quot;`
} `json:&quot;result&quot;`
}
Expand All @@ -681,17 +688,18 @@ func main() {
return
}

outputImageData, err := base64.StdEncoding.DecodeString(respData.Result.Image)
layoutImageData, err := base64.StdEncoding.DecodeString(respData.Result.LayoutImage)
if err != nil {
fmt.Println(&quot;Error decoding base64 image data:&quot;, err)
return
}
err = ioutil.WriteFile(outputImagePath, outputImageData, 0644)
err = ioutil.WriteFile(layoutImagePath, layoutImageData, 0644)
if err != nil {
fmt.Println(&quot;Error writing image to file:&quot;, err)
return
}
fmt.Printf(&quot;Image saved at %s.jpg\n&quot;, outputImagePath)
fmt.Printf(&quot;Image saved at %s.jpg\n&quot;, layoutImagePath)

fmt.Println(&quot;\nDetected formulas:&quot;)
for _, formula := range respData.Result.Formulas {
fmt.Println(formula)
Expand All @@ -713,7 +721,7 @@ class Program
{
static readonly string API_URL = &quot;http://localhost:8080/formula-recognition&quot;;
static readonly string imagePath = &quot;./demo.jpg&quot;;
static readonly string outputImagePath = &quot;./out.jpg&quot;;
static readonly string layoutImagePath = &quot;./layout.jpg&quot;;

static async Task Main(string[] args)
{
Expand All @@ -731,11 +739,11 @@ class Program
string responseBody = await response.Content.ReadAsStringAsync();
JObject jsonResponse = JObject.Parse(responseBody);

string base64Image = jsonResponse[&quot;result&quot;][&quot;image&quot;].ToString();
byte[] outputImageBytes = Convert.FromBase64String(base64Image);
string layoutBase64Image = jsonResponse[&quot;result&quot;][&quot;layoutImage&quot;].ToString();
byte[] layoutImageBytes = Convert.FromBase64String(layoutBase64Image);
File.WriteAllBytes(layoutImagePath, layoutImageBytes);
Console.WriteLine($&quot;Output image saved at {layoutImagePath}&quot;);

File.WriteAllBytes(outputImagePath, outputImageBytes);
Console.WriteLine($&quot;Output image saved at {outputImagePath}&quot;);
Console.WriteLine(&quot;\nDetected formulas:&quot;);
Console.WriteLine(jsonResponse[&quot;result&quot;][&quot;formulas&quot;].ToString());
}
Expand All @@ -749,7 +757,7 @@ const fs = require('fs');

const API_URL = 'http://localhost:8080/formula-recognition'
const imagePath = './demo.jpg'
const outputImagePath = &quot;./out.jpg&quot;;
const layoutImagePath = &quot;./layout.jpg&quot;;

let config = {
method: 'POST',
Expand All @@ -768,11 +776,13 @@ function encodeImageToBase64(filePath) {
axios.request(config)
.then((response) =&gt; {
const result = response.data[&quot;result&quot;];
const imageBuffer = Buffer.from(result[&quot;image&quot;], 'base64');
fs.writeFile(outputImagePath, imageBuffer, (err) =&gt; {

imageBuffer = Buffer.from(result[&quot;layoutImage&quot;], 'base64');
fs.writeFile(layoutImagePath, imageBuffer, (err) =&gt; {
if (err) throw err;
console.log(`Output image saved at ${outputImagePath}`);
console.log(`Output image saved at ${layoutImagePath}`);
});

console.log(&quot;\nDetected formulas:&quot;);
console.log(result[&quot;formulas&quot;]);
})
Expand All @@ -785,9 +795,9 @@ axios.request(config)

<pre><code class="language-php">&lt;?php

$API_URL = &quot;http://localhost:8080/formula-recognition&quot;;
$API_URL = &quot;http://localhost:8080/formula-recognition&quot;
$image_path = &quot;./demo.jpg&quot;;
$output_image_path = &quot;./out.jpg&quot;;
$layout_image_path = &quot;./layout.jpg&quot;

$image_data = base64_encode(file_get_contents($image_path));
$payload = array(&quot;image&quot; =&gt; $image_data);
Expand All @@ -801,8 +811,10 @@ $response = curl_exec($ch);
curl_close($ch);

$result = json_decode($response, true)[&quot;result&quot;];
file_put_contents($output_image_path, base64_decode($result[&quot;image&quot;]));
echo &quot;Output image saved at &quot; . $output_image_path . &quot;\n&quot;;

file_put_contents($layout_image_path, base64_decode($result[&quot;layoutImage&quot;]));
echo &quot;Output image saved at &quot; . $layout_image_path . &quot;\n&quot;;

echo &quot;\nDetected formulas:\n&quot;;
print_r($result[&quot;formulas&quot;]);

Expand Down
Loading

0 comments on commit 415ce5c

Please sign in to comment.