Go to achieve watermelon video pepper live and other platforms intelligent questions

Download code:

git clone https://github.com/Chain-Zhang/answer_ai.git
Copy the code

introduce

Recently, there have been a number of live streams where people who get 12 questions right in a row can split the prize pool with all the people who get them right, ranging from 200,000 to five million won. When this time, I realized that “books to time square hate less” this words of wisdom. Then I suddenly thought, Gee! We are not have have the Internet that knows, the title gets baidu in search not to be over. But I only had 10 seconds to answer the questions, and even though I had the speed of a single person for more than 20 years, I couldn’t do it. On second thought, what am I, a programmer? Do I have to do this myself?

So a baidu, found a big god of Java intelligent answer source, here the big god source address posted for your reference: github.com/lingfengsan…

However, I learned such a long time to go language, can use go to achieve it. So I gave it a try, along the same lines as the Java tools mentioned earlier. Here’s how I did it.

Train of thought

  1. The phone connects to the computer and opens the live broadcast page
  2. When the page is written, take a screenshot with ADB and save it to your computer
  3. Through Baidu AI word recognition, extract the title and option text in the picture
  4. Use Baidu search and then count the number of results obtained
  5. Compare the number of results found and sort
  6. For negative questions, choose the least number of options, and for positive questions, choose the most options.

The environment

hardware

  • A Windows PC
  • One Android phone
  • Android data line one

software

  • Golang development environment
  • Adb Android debug driver

other

  • Baidu AI developer platform to create a word recognition application

Environment set up

The hardware has nothing to say. This is mainly about software.

Golang development environment

Golang installation package (i386 for x86, AMD64 for X64)

After installing Go, it’s a good idea to check that all of the environment variables are working. The main environment variables are as follows:

  • GOROOT: Installation directory of Go
  • GOPATH: The directory used to store the Go language Package. This directory cannot be in the Go installation directory
  • GOBIN: Go directory for storing binary files, written as %GOROOT%\bin
  • PATH: You need to add %GOBIN% to the end of the PATH variable to make it easier to run Go from the command line

When finished, type in the CMD window:go version

As shown in the figure, the installation and configuration are successful.

And then there’s the IDE, which is even easier. You can use Notepad directly, of course, you can also use some lightweight editors, VScode, Vim are ok. You can also use goland, etc. These look at their own hobbies. I’m using vscode anyway.

The adb install

The full name of ADB is Android Debug Bridge. It is a Bridge between Android phones and PCS. You can use ADB to manage and operate emulators and devices, such as installing software, viewing hardware and software parameters of devices, upgrading systems, and running shell commands.

Here first give a download address: ADB download address (there are points big guys from me here, I have no points, want to earn points)

You can install it after downloading. Then configure the installed path into the environment variables, so that we can use adb commands in a CMD window. Once configured, you can run adb Devices from a CMD window:

As you can see, here we’ve started ADB and given a list of devices, but since I’m not connected to an Android device, nothing is displayed. At this point, we connected the Android phone to the computer with a cable and turned on the USB debug option on the phone. Settings -> Developer options ->USB debug, different brands of mobile phone may have differences, Baidu you know.

Sometimes you can do this and still not list your device. Then you need to do the following:

  1. Find your device in Device Management in Computer Management, and right click -> Properties -> Details -> find the hardware ID in the properties of details page, and then copy the hardware ID. My mobile phone is Meizu, and my hardware ID is: 2A45
  2. inC:\Users\ your username \.androidFind the adb_USb. ini file in the directory, if you do not create it yourself. Then write in the ID of the hardware you just copied. Since this ID is hexadecimal, prefix it with 0x, i.e. :0x2A45.
  3. Restart ADB, stop ADB:adb kill-server, start ADB:adb start-server. That should do it. If it still doesn’t work, please baidu yourself.

At this point, our environment is complete.

The experiment

Before the experiment, must be to download the source code, of course, there is no need to rely on the package. Here I used a bidu-AI-SDK package. To complete the installation, run the following command:

go get github.com/chenqinghe/baidu-ai-go-sdk
Copy the code

Then download my source code via Git:

git clone https://github.com/Chain-Zhang/answer_ai.git
Copy the code

Let’s look at the main function first

func main(){
	for {
		var cmd string
		fmt.Printf("> ")
		fmt.Scan(&cmd)
		switch cmd{
		case "1":
			ai.Start()
		case "2":
            ai.ExeCommand("cmd", []string{"/c", "adb", "devices"})
		case "exit":
			os.Exit(1)
		}
	}
}
Copy the code

As you can see from the code, the program waits for user input while running.

  1. When the input1Will be screenshots of the operation.
  2. When the input2Will list the devices connected to the computer
  3. When the inputexitWill exit the program.

Let’s go to our code directory in the CMD window and execute the following command to run our program:

go run main.go
Copy the code

Then enter 2 to see if any devices are connected:

Then the mobile phone opens the live broadcast. When the host sets the question, enter 1 and press Enter. Here is the experiment, so the mobile phone directly opens a picture.

After a series of analyses, the following results are returned:

According to the negative question choose the smallest number of choices, the affirmative question choose the largest number of choices so this question choose: 2-C miles C miles.