[PYTHON] Problems with output results with Google's Cloud Vision API

When I tried to output a JSON file, there were strange characters at the beginning and end of the file.

In the first place, I thought that I didn't understand the function, so I realized that I had to understand it properly.

The site I was referring to

I made a base with reference to here. [I tried using Google Cloud Vision](https://www.itbook.info/web/2016/11/google-cloud-vision%E3%82%92%E4%BD%BF%E3%81%A3% E3% 81% A6% E3% 81% BF% E3% 81% 9F.html)

What i was doing

In the first place, what I was doing was outputting the response result obtained by hitting the Cloud Vison API as a file with pickle.dump. Then, weird characters such as ".X ~" were entered at the beginning, and "q." Was entered at the end, which was a problem. Therefore, json.load could not be done for the output file.


response = requests.post(ENDPOINT_URL
   ,data=json.dumps({"requests": img_requests}).encode()
                             ,params={'key': api_key}
                             ,headers={'Content-Type': 'application/json'})
        
result = json.dumps(response.json()['responses'], ensure_ascii=False, indent=4)
print(result)
f = open("./output.json", 'wb')
pickle.dump(result, f)

Solution

After changing the process as follows, I was able to output it in a form that can be read by json.load.


f = open("./output.json", 'wb')
f.write(result.encode("UTF-8"))



I thought I wrote this and tried it below, Same as the first problem. Lack of understanding. I want to add it when I understand it. Is it because it was in binary format? Anyway, investigation.


f = open("./output.json", 'wb')
f.write(result.encode("UTF-8"))

that's all.

Recommended Posts

Problems with output results with Google's Cloud Vision API
Text extraction with GCP Cloud Vision API (Python3.6)
Flow of extracting text in PDF with Cloud Vision API
Google Cloud Vision API sample for python
Streaming speech recognition with Google Cloud Speech API
Use Google Cloud Vision API from Python
Transcription of images with GCP's Vision API
Machine Learning x Web App Diagnosis: Recognize CAPTCHA with Cloud Vision API
Detect Japanese characters from images using Google's Cloud Vision API in Python
How to use GCP's Cloud Vision API
Transcribe WAV files with Cloud Speech API
I tried "License OCR" with Google Vision API
Automatic voice transcription with Google Cloud Speech API
I tried using the Google Cloud Vision API
I tried "Receipt OCR" with Google Vision API
Get data labels by linking with Google Cloud Vision API when previewing images with Rails
Recent Ability of Image Recognition-MS State-of-the-art Research Results Using Computer Vision API with Python
[GCP] [Python] Deploy API serverless with Google Cloud Functions!
Output address transactions with Ethereum's Explorer (Etherscan) API (Ethereum)
Output 2D thermal diffusion simulation results with Python VTK
Text extraction (Read API) with Azure Computer Vision API (Python3.6)
A story of reading a picture book by synthesizing voice with COTOHA API and Cloud Vision API