Some time ago, answer APP in full swing development, major Internet companies have joined the coin war, including such as Chongding conference, Million hero, Cheese hero and so on. Following is also the rise of each answer application assistance.

There are already a number of online quiz apps, which generally involve two steps: getting the question options and searching for the answer. Capturing the titles and options involves capturing a screen shot of the phone with ADB, and then recognising the titles and options with optical Character recognization (OCR). Most of the OCR tools used are Google’s tesseract-OCR and Baidu’s OCR API. Google tesseract – OCR can be installed in the local, software download address is https://digi.bib.uni-mannheim.de/tesseract/tesseract-ocr-setup-3.05.01.exe, Choose to add simplified Chinese language package during installation; otherwise, Chinese cannot be recognized. Another method is to use Baidu’S OCR API, which can be applied for free and is more convenient to use, and the recognition rate is relatively more accurate. Another advantage of the Baidu API is that images can be recognized without processing, whereas TesserACt-OCR generally requires simple processing of images. Another way to get questions and options is to use the package capture tool to grab APP requests to get questions and options information.

On the other hand, search for answers to questions. Several common methods are to open the browser directly with the topic as the search keyword, or a question plus options search, to obtain the number of search engine results. Generally speaking, the answers obtained in this way are not accurate. Firstly, the way of setting questions is becoming more and more weird. Secondly, the more relevant the questions are, the more correct the answers are. Originally, it is very difficult to judge the questions and choices. Unless you can make a perfect semantic understanding, it is difficult to judge the correct choice. Another straightforward way is to set up a question bank. In this article, we discuss a way to build a question bank. This is just a simple exploration, which may not be practical, because the question bank must be comprehensive enough to be effective.

Create item library using ElasticSearch

This article mainly explains the establishment of the question bank on a very small aspect of exploration, for the use of answer assistance can read the original text to see the complete introduction, the code is mainly based on TopSup made some adjustments. Elasticsearch will be used to create the question bank, see the first article for installing ES. Some people may think that using ES for the question bank is like shooting an anti-aircraft gun at a mosquito — making a mountain out of a molehill. But I found ES easy to install and use, and thanks to its powerful RESTFUL interface, you can manipulate ES with almost any tool. Talk is cheap, show me the code.

from elasticsearch import Elasticsearch

def write_quetion(a):
  question = {
    'question': 'Who's the most handsome man in the world?'.'answer': 'Neal'
  }
  es = Elasticsearch({'localhost'})
  es.index(index='question-index', doc_type='question', id=1, body=question)
Copy the code

Here’s a simple code snippet that looks like writing a record to an index. In fact, ES is a non-relational database, and in DB-Engines’ latest rankings, ES has shot up to no. 9. Some of the concepts in Elasticsearch can be compared to relational databases:

Relational database Elasticsearch
database index
table type
row document
column field

So when searching for a question in ES, it should look like this:

def search_question(key_words):
  es = Elasticsearch({'localhost'})
  res = es.search(index='question-index', body={
    "query": {
      "match": {
        "question": key_words,
        "minimum_should_match": "75%"}}}})if res['hits'['total'] > 0:
    for hit in res['hits'] ['hits']:
      print(hit['_source'] ['question'] + ':' + hit['_source'] ['answer'])
   else:
     print('No similar results found')
Copy the code

Get questions and answers from the pictures

Question bank can be established by using text or directly using the phone screenshots of the answering app, the latter is undoubtedly more valuable. Suppose we now have a screenshot like this:

The picture already contains the correct answer, but how do we recognize the picture and know the correct answer? Use the number after the choice? No, the correct answer is not necessarily the most chosen choice. Thanks to the image processing course, there is a very basic concept that helped me solve this problem. Generally speaking, the transformation of color image into grayscale image is to map color space to grayscale space through a certain function. Taking the RGB2Gray function of MATLAB that converts RGB graph (which can be understood as a color graph) into grayscale graph as an example, suppose that the RGB value of a color pixel is (R, G, B), then its grayscale value G should be calculated as follows:

G = 0.2989 * R + 0.5870 * G + 0.1140 * B

The general practice in the industry is to calculate the gray value of color pixels according to a certain weight. By taking the color pen, the RGB value of the correct answer background color is (80, 215, 216), while the RGB value of the wrong answer background color is (194, 194, 194).

Today we’re going to teach you the distributive property, which shows you a wave of elementary school math. Without further ado, it can be seen that color image maps have a lower gray level. This is a big help in distinguishing the right choices from the wrong ones. First, we clipped the option area to avoid the number on the right affecting the recognition result. Through the binarization algorithm, we can convert the image of the problem option graph into two different images with different thresholds. The pixels smaller than the threshold value are turned into black pixels, and the pixels larger than the threshold value are programmed with white pixels. The binary conversion algorithm is very simple:

def binarizing(img, threshold):
    pixdata = img.load()
    w, h = img.size
    for y in range(h):
        for x in range(w):
            if pixdata[x, y] < threshold:
                pixdata[x, y] = 0
            else:
                pixdata[x, y] = 255
    return img
Copy the code

Binarization images are obtained by thresholds 120 and 180(any value between 175 and 194 is ok), and the results are as follows:

Well, there you have it. We identify these two images by OCR method, the first image can get all the options, while the second image can only get the wrong options, so the difference between the two is the correct option! Is it strange bones, is it unexpected!

conclusion

That’s it for this article, which focuses on a way to build a question bank from a very small perspective, using a simple technique of image processing to get the right answer. Do you think the courses you learned are worthwhile? Of course, this article is only as a technical discussion, does not guarantee the actual operability, the detailed code can read the original view.

The above.

Please search the wechat id mad_coder or scan the QR code to follow the official account: