Haha, I developed a magic search question using Python

Haha, I developed a magic search question using Python

Professional search for questions, a good helper for parents

I have done a picture recognition project a long time ago. At that time, there was a function of whole problem recognition, that is, a screenshot of the incoming math problem, and the content of the picture can be recognized through ocr technology, but at that time it was limited to recognizing text and did not make a further step. Now I think it s not very practical. After all, what everyone needs is the idea of solving the problem, instead of letting the AI read the problem stem (I know the text of the problem stem, and I don t know how to do it. = = ), I just had time recently, so I tried to make a question search tool for friends with babies.

In view of the good experience of using Youdao Zhiyun in the development of the whole question recognition, I opened its official document again, and I found the open API of the photo search service service, and made a simple batch search demo with a familiarity . Let s share the development below. process.

Preparation for calling the API interface

First of all, it is necessary to create an instance, create an application, bind the application and the instance on the personal page of Youdao Zhiyun, and obtain the id and key of the application. The specific individual registration process and application creation process are detailed in the article

Detailed introduction to the development process

The following describes the specific code development process.

The parameters received by the API are relatively simple:

Field nameTypes ofmeaningRequiredRemarks
qtextThe picture to be recognized requires Base64 encodingTrueMust be Base64 encoded (do not add data:image/png;base64 before baes64)
appKeytextApp IDTrueCan be viewed in the application management
salttextUUIDTrueuuid
curtimetextCurrent UTC timestamp (seconds)trueTimeStamp
signtextSignature sha256 (application ID+input+salt+curtime+application key); see the remarks under the table for the rules of input generationTruesha256 (application ID+input+salt+curtime+application key)
signTypetextSignature typetruev2
typetextUpload type, only base64 upload is supported, please fill in a fixed value 1True1
searchTypetextSearch type, img is picture search question, text is text search questionfalseimg

signature

sign
The generation method is as follows:
signType=v2;
sign=sha256(
App ID
+
input
+
salt
+
curtime
+
Application key
).
Among them, the calculation method of input is:
input
=
q first 10 characters
+
q length
+
10 characters after q
(When the length of q is greater than 20) or
input
=
q string
(When the length of q is less than or equal to 20).

It should be noted that the API has the following requirements for the subject image:

ruledescription
transfer methodHTTPS
Request methodPOST
Character EncodingUnified use of UTF-8 encoding
Request formatForm
Response formatJSON
Image Formatjpg/png/bmp
Image size1MB or less
Text lengthLess than 50 characters

Demo development:

This demo is developed using python3, including maindow.py, QuestionClass.py, OcrQuestion.py three files, which are the demo interface, interface logic processing and ocr search method package.

  1. Interface part:

    The UI part is relatively simple, and the main function is to select the picture to be questioned and select the storage path of the correction result. The layout code is as follows:

    root=tk.Tk() root.title( "youdao ocr question test" ) frm = tk.Frame(root) frm.grid(padx = '50' , pady = '50' ) # Select topic and save result button btn_get_file = tk.Button(frm, text = 'Select topic picture' , command=get_files) btn_get_file.grid(row = 0 , column = 0 , ipadx = '3' , ipady = '3' , padx = '10' , pady = '20' ) text1 = tk.Text(frm, width = '40' , height = '10' ) text1.grid(row = 0 , column = 1 ) btn_get_result_path=tk.Button(frm,text= 'Select the search result path' ,command=set_result_path) btn_get_result_path.grid(row = 1 ,column = 0 ) text2=tk.Text(frm,width = '40' , height = '2' ) text2.grid(row = 1 ,column = 1 ) # Search title button btn_sure=tk.Button(frm,text= "search title" ,command=search_question_files) btn_sure.grid(row = 4 , column = 1 ) root.mainloop() Copy code

    The binding event search_question_files() of the start button btn_sure is used to search for questions based on the title photos, and the result storage path is opened after completion:

    def search_question_files (): question.start_ocr() the os.system ( 'Start' + question.result_path) copying the code
  2. QuestionClass.py

    This is mainly in line with the logic of the UI to call the search method.

    First define a class Question:

    class Question (): DEF the __init__ ( Self, file_paths, result_path ): self.file_paths = file_paths # Title pictures stored path self.result_path = result_path # result path copy the code

    The start_ocr() method calls the connect() method to search for questions in turn and save the results.

    def start_ocr ( self ): for file_path in self.file_paths: result=connect(file_path) print (file_path) self.save_result_format(file_path,result) Copy code

    The result obtained from the connect method of OcrQuestion.py is in json format. The save_result_format() method parses the interface obtained from the interface, organizes the format, and saves the result to html:

    def save_result_format ( self,file_path,result ): result_file_name=os.path.basename(file_path).split( '.' )[ 0 ]+ '_result.html' f = open (self.result_path+ '/' +result_file_name, ' w' ,encoding = 'utf-8' ) result_json = json.loads(result) if result_json[ 'errorCode' ] == '0' : data=result_json[ 'data' ] questions=data[ "questions" ] text=data[ "text" ] f.write( "Title recognition:<br/>" +text) i = 0 for answers in questions: i=i+ 1 subject= "Subject:" +answers[ "subject" ]+ "<br>" answer= "Answer:" +answers[ "answer" ]+ "<br>" analysis= "Analysis:" +answers [ "analysis" ]+ "<br>" knowledge= "Knowledge points:" +answers[ "knowledge" ]+ "<br>" print (subject+answer+analysis+knowledge) result_each= "<h3>Search results" + str (i)+ "<br></h3>" result_each=result_each+subject+answer+analysis+knowledge+ "<br>========== =======This is a separator ============<br>" f.write(result_each) else : f.write ( "Result error code:" + result_json [ 'errorCode' ]) Copy the code
  3. OcrQuestion.py

    OcrQuestion.py encapsulates the methods for requesting ocr search questions API, the most important method is connect():

    def connect ( pic_path ): f = open (pic_path, 'rb' ) # Open the graph file in binary mode q = base64.b64encode(f.read()).decode( 'utf-8' ) # Read the file content and convert Encode for base64 f.close() data = {} data[ 'q' ] = q data[ 'signType' ] = 'v2' curtime = str ( int (time.time())) data[ 'curtime' ] = curtime salt = str (uuid.uuid1()) signStr = APP_KEY + truncate(q) + salt + curtime + APP_SECRET sign = encrypt(signStr) data[ 'appKey' ] = APP_KEY data[ 'salt' ] = salt data[ 'sign' ] = sign response = do_request(data) = response.content.decode Result ( 'UTF-. 8' ) Print (Result) return Result duplicated code
    API response result example
    { "data" :{ "questions" :[ { "score" : 0.9875 , "answer" : "D" , "subject" : "History" , "id" : "a9db8f1252778836c99204e5cf9d7738" , "analysis" : "" , "type" : "" , "content" : "Scholar They believe that the May Fourth Movement marked a great awakening of Chinese national consciousness, which was manifested in <br/>A. Modern national democratic ideas began to spread B. Unveiled the prelude to the anti-imperialist and anti-feudal struggle<br/>C. Opened the modern era Chinese ideological enlightenment D. Extensive mobilization and participation of people from all walks of life" , "knowledge" :"" }, { "score" : 0.9875 , "answer" : "D <br>Pay attention to the topic "The Great Awakening of Chinese National Awareness", combined with what we have learned, students, workers, and businessmen were all involved in the May Fourth Movement. That is to say, the extensive mobilization and participation of people from all walks of life is in line with the meaning of the question, so item D is correct. <br>Before and after the Reform Movement of 1898, the Chinese national democratic thought has already begun to spread, so item A is wrong. <br>The May Fourth Movement is a counter- The patriotic movement against imperialism and feudalism was not the beginning of the struggle against imperialism and feudalism. In this period, the revolutionary program for anti-imperialist and feudalism was not clearly put forward, so item B was wrong. <br>Modern China spread democracy during the New Culture Movement. Thought, enlightenment of thought, so item C was wrong. <br>So I chose D." , "subject" : "History" , "id" : "2f8b9c814f064131cc7b2bead54f15da" , "analysis" : "This question examines the May Fourth Movement. Examination The significance of the May Fourth Movement. It examines students' ability to remember and use basic knowledge." , "type" : "Multiple choice", "content" :"Scholars believe that the May 4th Movement marked a great awakening of Chinese national consciousness, which is shown in () <p class=" question_option ">A. Modern national democratic ideas began to spread<br/>B. Revealed anti-imperialist and anti-feudalism The prelude to the struggle<br/>C. Opened the ideological enlightenment of modern China<br/>D. The extensive mobilization and participation of people from all walks of life</p>" "knowledge" : "/Human political civilization development history/Modern China The trend of anti-aggression and seeking democracy/May Fourth Movement", }, { "score" : 0.9875 , "answer" : "D <br>[answer] <p>D</p> <p></p>" , "subject" : "history" , "id" : "48a28969d882f20b04ffec15c8f1b94c" , "analysis" : "<p>The May Fourth Movement, which was asked by the title, began to spread before and after the Reform Movement of 1898, so A is incorrect. The May Fourth Movement is a patriotic movement against imperialism and feudalism, but it is not At the beginning of the struggle against imperialism and feudal feudalism, during this period, the revolutionary program for anti-imperialist and feudal feudalism was not clearly put forward, so B is incorrect. In modern China, democratic thoughts were spread during the New Culture Movement and ideological enlightenment was carried out, so C was incorrect. During the May Fourth Movement, students, workers, and businessmen were all involved, so D is correct.</p> <p> </p>" , "type" : "Multiple choice" , "content" : "<p>Scholars believe that the May Fourth Movement marked a great awakening of Chinese national consciousness, which is manifested in</p> <p></p> <p class="question_option ">A. Modern national democratic ideas began to spread<br/>B. Unveiled the prelude to the anti-imperialist and anti-feudal struggle<br/>C. Opened the ideological enlightenment of modern China<br/>D. People from all walks of life Extensive mobilization and participation</p>" , "knowledge" "/The history of the development of human political civilization/The trend of anti-aggression and seeking democracy in modern China/The First National Congress of the Communist Party of China": } ], "text" : "Scholars believe that the May Fourth Movement marked the great awakening of Chinese national consciousness, which is manifested in A. Modern national democratic ideas began to spread. B opened the prelude to the anti-imperialist and anti-feudal struggle. C opened the thoughts of modern China. Enlightenment D. Extensive mobilization and participation of people from all walks of life" }, "errorCode" : "0" } Copy code

    The response result is output in json format, and the included fields are shown in the following table:

Fieldmeaning
errorCodeThe error code of the recognition result must exist. Detailed information can be found Error Code List
datadata
-textPicture title OCR results
-questionsRelated topics
Idanswer
ContentSubject content
Answeranswer
AnalysisParsing
KnowledgeKnowledge points

Show results

demo operation demonstration

Take a look at the results:

Display of math problem search results:

History question search results

summary

Youdao Zhiyun s overall search API has clear documentation, a wide range of topics, and the ability to automatically determine subjects. The search results can be described as "inferring from one another", and several possible similar topics will be returned, which is of reference value and is worth recommending! Mathematics answering questions will return related pictures, formulas, etc., which is very good when used in web projects.

Project address: github.com/LemonQH/Ocr...