├── results.pdf ├── images ├── cm_hw.jpg ├── dog.jpg ├── graph.jpg ├── limit.jpg ├── algebra.jpg ├── diagram.jpg ├── vec_hw.jpg ├── cases_hw.jpg ├── fraction.jpg ├── grading_0.jpg ├── grading_1.jpg ├── hline_hw_0.jpg ├── hline_hw_1.jpg ├── integral.jpg ├── integral_2.jpg ├── inverted.jpg ├── logic_hw_0.jpg ├── matrix_2x2.jpg ├── matrix_3x3.jpg ├── text_hw_0.jpg ├── determinant.jpg ├── cases_printed_0.jpg ├── cases_printed_1.jpg ├── cases_printed_2.jpg ├── hline_printed_0.jpg ├── hline_printed_1.jpg ├── logic_printed_0.jpg ├── long_division.jpg ├── long_division_2.jpg ├── mixed_text_math.jpg ├── problem_number.jpg ├── text_printed_0.jpg ├── vec_printed_0.jpg ├── degrees_printed_0.jpg ├── foreign_printed_0.jpg ├── foreign_printed_1.jpg ├── foreign_printed_2.jpg ├── overline_printed.jpg ├── multiple_equations.jpg └── matrix_multiplication.jpg ├── python ├── README.md ├── simple.py ├── mathpix.py ├── formats.py └── batch.py └── README.md /results.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mathpix/api-examples/HEAD/results.pdf -------------------------------------------------------------------------------- /images/cm_hw.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mathpix/api-examples/HEAD/images/cm_hw.jpg -------------------------------------------------------------------------------- /images/dog.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mathpix/api-examples/HEAD/images/dog.jpg -------------------------------------------------------------------------------- /images/graph.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mathpix/api-examples/HEAD/images/graph.jpg -------------------------------------------------------------------------------- /images/limit.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mathpix/api-examples/HEAD/images/limit.jpg -------------------------------------------------------------------------------- /images/algebra.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mathpix/api-examples/HEAD/images/algebra.jpg -------------------------------------------------------------------------------- /images/diagram.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mathpix/api-examples/HEAD/images/diagram.jpg -------------------------------------------------------------------------------- /images/vec_hw.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mathpix/api-examples/HEAD/images/vec_hw.jpg -------------------------------------------------------------------------------- /images/cases_hw.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mathpix/api-examples/HEAD/images/cases_hw.jpg -------------------------------------------------------------------------------- /images/fraction.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mathpix/api-examples/HEAD/images/fraction.jpg -------------------------------------------------------------------------------- /images/grading_0.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mathpix/api-examples/HEAD/images/grading_0.jpg -------------------------------------------------------------------------------- /images/grading_1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mathpix/api-examples/HEAD/images/grading_1.jpg -------------------------------------------------------------------------------- /images/hline_hw_0.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mathpix/api-examples/HEAD/images/hline_hw_0.jpg -------------------------------------------------------------------------------- /images/hline_hw_1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mathpix/api-examples/HEAD/images/hline_hw_1.jpg -------------------------------------------------------------------------------- /images/integral.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mathpix/api-examples/HEAD/images/integral.jpg -------------------------------------------------------------------------------- /images/integral_2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mathpix/api-examples/HEAD/images/integral_2.jpg -------------------------------------------------------------------------------- /images/inverted.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mathpix/api-examples/HEAD/images/inverted.jpg -------------------------------------------------------------------------------- /images/logic_hw_0.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mathpix/api-examples/HEAD/images/logic_hw_0.jpg -------------------------------------------------------------------------------- /images/matrix_2x2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mathpix/api-examples/HEAD/images/matrix_2x2.jpg -------------------------------------------------------------------------------- /images/matrix_3x3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mathpix/api-examples/HEAD/images/matrix_3x3.jpg -------------------------------------------------------------------------------- /images/text_hw_0.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mathpix/api-examples/HEAD/images/text_hw_0.jpg -------------------------------------------------------------------------------- /images/determinant.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mathpix/api-examples/HEAD/images/determinant.jpg -------------------------------------------------------------------------------- /images/cases_printed_0.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mathpix/api-examples/HEAD/images/cases_printed_0.jpg -------------------------------------------------------------------------------- /images/cases_printed_1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mathpix/api-examples/HEAD/images/cases_printed_1.jpg -------------------------------------------------------------------------------- /images/cases_printed_2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mathpix/api-examples/HEAD/images/cases_printed_2.jpg -------------------------------------------------------------------------------- /images/hline_printed_0.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mathpix/api-examples/HEAD/images/hline_printed_0.jpg -------------------------------------------------------------------------------- /images/hline_printed_1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mathpix/api-examples/HEAD/images/hline_printed_1.jpg -------------------------------------------------------------------------------- /images/logic_printed_0.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mathpix/api-examples/HEAD/images/logic_printed_0.jpg -------------------------------------------------------------------------------- /images/long_division.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mathpix/api-examples/HEAD/images/long_division.jpg -------------------------------------------------------------------------------- /images/long_division_2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mathpix/api-examples/HEAD/images/long_division_2.jpg -------------------------------------------------------------------------------- /images/mixed_text_math.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mathpix/api-examples/HEAD/images/mixed_text_math.jpg -------------------------------------------------------------------------------- /images/problem_number.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mathpix/api-examples/HEAD/images/problem_number.jpg -------------------------------------------------------------------------------- /images/text_printed_0.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mathpix/api-examples/HEAD/images/text_printed_0.jpg -------------------------------------------------------------------------------- /images/vec_printed_0.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mathpix/api-examples/HEAD/images/vec_printed_0.jpg -------------------------------------------------------------------------------- /images/degrees_printed_0.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mathpix/api-examples/HEAD/images/degrees_printed_0.jpg -------------------------------------------------------------------------------- /images/foreign_printed_0.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mathpix/api-examples/HEAD/images/foreign_printed_0.jpg -------------------------------------------------------------------------------- /images/foreign_printed_1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mathpix/api-examples/HEAD/images/foreign_printed_1.jpg -------------------------------------------------------------------------------- /images/foreign_printed_2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mathpix/api-examples/HEAD/images/foreign_printed_2.jpg -------------------------------------------------------------------------------- /images/overline_printed.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mathpix/api-examples/HEAD/images/overline_printed.jpg -------------------------------------------------------------------------------- /images/multiple_equations.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mathpix/api-examples/HEAD/images/multiple_equations.jpg -------------------------------------------------------------------------------- /images/matrix_multiplication.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mathpix/api-examples/HEAD/images/matrix_multiplication.jpg -------------------------------------------------------------------------------- /python/README.md: -------------------------------------------------------------------------------- 1 | # Usage: 2 | 3 | Do: 4 | ``` 5 | cd api-examples/python 6 | python3 simple.py 7 | python3 formats.py 8 | ``` 9 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # API Examples 2 | 3 | ## Docs 4 | 5 | Some API examples for the image to Latex API that powers mathpix.com as well as others. The general docs for the API can be found here: http://docs.mathpix.com 6 | 7 | The purpose of this repo is to provide illustrative examples for various platforms. Please use https://github.com/mathpix/ios-sample for a sample iOS app and use https://github.com/mathpix/android-sample for an Android one. 8 | 9 | Note: for production use, please request an API key from support@mathpix.com. 10 | -------------------------------------------------------------------------------- /python/simple.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | import mathpix 4 | import json 5 | 6 | # 7 | # Simple example of calling Mathpix OCR with ../images/algebra.jpg. 8 | # 9 | # We use the default ocr (math-only) and a single return format, latex_simplified. 10 | # 11 | # If you expect the image to be math and want to examine the result 12 | # as math then the return format should either be latex_simplified, 13 | # asciimath, or mathml. If you want to see the text in the image then 14 | # you should include 'ocr': ['math', 'text'] as an argument and 15 | # the return format should be either text or latex_styled 16 | # (depending on whether you want to use the result as a paragraph or an equation). 17 | # 18 | 19 | r = mathpix.latex({ 20 | 'src': mathpix.image_uri('../images/algebra.jpg'), 21 | 'formats': ['latex_simplified'] 22 | }) 23 | 24 | print(json.dumps(r, indent=4, sort_keys=True)) 25 | assert(r['latex_simplified'] == '12 + 5 x - 8 = 12 x - 10') 26 | -------------------------------------------------------------------------------- /python/mathpix.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | import os 4 | import base64 5 | import requests 6 | import json 7 | 8 | # 9 | # Common module for calling Mathpix OCR service from Python. 10 | # 11 | # N.B.: Set your credentials in environment variables APP_ID and APP_KEY, 12 | # either once via setenv or on the command line as in 13 | # APP_ID=my-id APP_KEY=my-key python3 simple.py 14 | # 15 | 16 | env = os.environ 17 | 18 | default_headers = { 19 | 'app_id': env.get('APP_ID', 'trial'), 20 | 'app_key': env.get('APP_KEY', '34f1a4cea0eaca8540c95908b4dc84ab'), 21 | 'Content-type': 'application/json' 22 | } 23 | 24 | service = 'https://api.mathpix.com/v3/latex' 25 | 26 | # 27 | # Return the base64 encoding of an image with the given filename. 28 | # 29 | def image_uri(filename): 30 | image_data = open(filename, "rb").read() 31 | return "data:image/jpg;base64," + base64.b64encode(image_data).decode() 32 | 33 | # 34 | # Call the Mathpix service with the given arguments, headers, and timeout. 35 | # 36 | def latex(args, headers=default_headers, timeout=30): 37 | r = requests.post(service, 38 | data=json.dumps(args), headers=headers, timeout=timeout) 39 | return json.loads(r.text) 40 | -------------------------------------------------------------------------------- /python/formats.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | import mathpix 4 | import json 5 | 6 | # 7 | # Example using Mathpix OCR with multiple result formats. We want to recognize 8 | # both math and text in the image, so we pass the ocr parameter set to 9 | # ['math', 'text']. This example returns the LaTeX text format, which 10 | # starts in text mode instead of math mode, the latex_styled format, 11 | # the asciimath format, and the mathml format. We define custom 12 | # math delimiters for the text result so that the math is surrounded 13 | # by dollar signs ("$"). 14 | # 15 | 16 | r = mathpix.latex({ 17 | 'src': mathpix.image_uri('../images/mixed_text_math.jpg'), 18 | 'ocr': ['math', 'text'], 19 | 'skip_recrop': True, 20 | 'formats': ['text', 'latex_styled', 'asciimath', 'mathml'], 21 | 'format_options': { 22 | 'text': { 23 | 'transforms': ['rm_spaces', 'rm_newlines'], 24 | 'math_delims': ['$', '$'] 25 | }, 26 | 'latex_styled': {'transforms': ['rm_spaces']} 27 | } 28 | }) 29 | 30 | # 31 | # Note the actual results might be slighly different in LaTeX spacing or 32 | # MathML attributes. 33 | # 34 | 35 | print('Expected for r["text"]: "$-10 x^{2}+5 x-3$ and $-7 x+4$"') 36 | print('Expected for r["latex_styled"]: "-10 x^{2}+5 x-3 \\text { and }-7 x+4"') 37 | print('Expected for r["asciimath"]: "-10x^(2)+5x-3\\" and \\"-7x+4"') 38 | print('Expected for r["mathml"]: "\u221210x2+5x\u22123\u00a0and\u00a0\u22127x+4"') 39 | 40 | print("\nResult object:", json.dumps(r, indent=4, sort_keys=True)) 41 | -------------------------------------------------------------------------------- /python/batch.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | import os 3 | import sys 4 | import time 5 | import requests 6 | import json 7 | 8 | # 9 | # Batch request example in Python. 10 | # 11 | 12 | # get api_key from dashboard.mathpix.com or contact support@mathpix.com if you're in China 13 | server = os.environ.get('MATHPIX_API', 'https://api.mathpix.com') 14 | app = os.environ['MATHPIX_APP_ID'] 15 | key = os.environ['MATHPIX_APP_KEY'] 16 | headers={'app_id': app, 'app_key': key, 'Content-type': 'application/json'} 17 | 18 | # 19 | # Flag indicating whether you are running in a terminal and want to display 20 | # partial progress. 21 | # 22 | interactive = True 23 | 24 | urlbase = "https://raw.githubusercontent.com/Mathpix/api-examples/master/images/" 25 | images = [ 26 | 'algebra.jpg', 'fraction.jpg', 'graph.jpg', 'integral.jpg', 27 | 'limit.jpg', 'long_division.jpg', 'matrix_2x2.jpg', 'matrix_3x3.jpg', 28 | 'mixed_text_math.jpg', 'multiple_equations.jpg' 29 | ] 30 | n = len(images) 31 | 32 | urls = {} 33 | for i, img in enumerate(images): 34 | urls['url-' + str(i + 1).zfill(2)] = urlbase + img 35 | 36 | body = {'urls': urls, 'formats': ['latex_normal']} 37 | start = time.time() 38 | r = requests.post(server + '/v3/batch', headers=headers, data=json.dumps(body)) 39 | info = json.loads(r.text) 40 | print info 41 | b = info['batch_id'] 42 | print("Batch id is %s" % b) 43 | 44 | # 45 | # Polling frequency is based on a guess of how long the batch will take. 46 | # One second per batch item is quite conservative but actual time depends 47 | # on non-batch request traffic because batch requests are lower priority. 48 | # 49 | # We use a maximum wait time for interactive output. Server-side applications 50 | # without screen output should just use an estimate. 51 | # 52 | progress = 0 53 | estimate = n 54 | if interactive: 55 | maxwait = 2.0 56 | 57 | while True: 58 | timeout = float(estimate) 59 | if interactive: 60 | pct = float(100 * progress) / n 61 | sys.stdout.write('\r{0:5.1f}% {1}/{2}\033[K'.format(pct, progress, n)) 62 | sys.stdout.flush() 63 | timeout = min(timeout, maxwait) 64 | 65 | time.sleep(timeout) 66 | 67 | r = requests.get(server + '/v3/batch/' + b, headers=headers) 68 | current = json.loads(r.text) 69 | results = current['results'] 70 | progress = len(results) 71 | if progress == n: 72 | if interactive: 73 | print('\r{0:5.1f}% {1}/{2}'.format(100.0, progress, n)) 74 | 75 | print('Batch results:') 76 | for key in sorted(results): 77 | result = results[key] 78 | answer = result.get('latex_normal', '') or result.get('error', '???') 79 | print(key + ': ' + answer) 80 | 81 | break 82 | 83 | # Adjust estimate based on how many items still need processing. 84 | if progress > 0: 85 | estimate = float(n - progress) * (time.time() - start) / float(progress) 86 | --------------------------------------------------------------------------------