思路: 阿里云图像识别 + 语音模块实现
主程序:
头文件
garbage.h
#ifndef __GARBAGE__H
#define __GARBAGE__H
void garbage_init(void);
void garbage_final(void);
char *garbage_category(char *category);
//为什么能用 127.0.0.1 我们是vscode 远程连接到香橙派 -- 实际上是在orangepi上运行,用本地ip即可
#define WGET_CMD "wget http://127.0.0.1:8080/?action=snapshot -O/tmp/garbage.jpg"
#define GARBAGE_FILE "/tmp/garbage.jpg"
#endif
uartTool.h
#ifndef __UARTTOOL_H
#define _UARTTOOL_H
int myserialOpen (const char *device, const int baud);
void serialSendstring (const int fd, const unsigned char *s,int len);
int serialGetstring (const int fd,unsigned char *buffer);
//设置串口5 和 波特率 115200
#define SERIAL_DEV "/dev/ttyS5"
#define BAUD 115200
#endif
其他源文件:
garbage.c
#include<Python.h>
#include "garbage.h"
void garbage_init(void)
{
Py_Initialize();
PyObject *sys = PyImport_ImportModule("sys");
PyObject *path = PyObject_GetAttrString(sys,"path");
PyList_Append(path, PyUnicode_FromString("."));
}
void garbage_final(void)
{
Py_Finalize();
}
char *garbage_category(char *category)
{
PyObject *pModule= PyImport_ImportModule("garbage");
if(!pModule)
{
PyErr_Print();
printf("Error: failed to load garbage.py\n");
goto FAILED_MODULE;
}
PyObject *pfunc =PyObject_GetAttrString(pModule,"alibaba_garbage");
if(!pfunc)
{
PyErr_Print();
printf("Error: failed to load alibaba_garbage\n");
goto FAILED_FUNC;
}
PyObject *pValue = PyObject_CallObject(pfunc, NULL);
if(!pValue)
{
PyErr_Print();
printf("Error:function call failed\n");
goto FAILED_VALUE;
}
char * result = NULL;
if(!PyArg_Parse(pValue,"s",&result))
{
PyErr_Print();
printf("Error:garbage failed\n");
goto FAILED_RESULT;
}
printf("result=%s\n",result );
// pValue 下面会被释放,他的result值就拿不到,得先存起来
category = (char*)malloc(sizeof(char)*(strlen(result)+1));
memset(category,0,(strlen(result)+1));
strncpy(category,result,(strlen(result)+1));
//注意释放顺序是反的,从新到旧
// 添加跳转位置,当发生错误的时候,跳转过来将他释放
FAILED_RESULT:
Py_DECREF(pValue);
FAILED_VALUE:
Py_DECREF(pfunc);
FAILED_FUNC:
Py_DECREF(pModule);
FAILED_MODULE:
return category;
}
uartTool.c
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdarg.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "wiringSerial.h"
int myserialOpen (const char *device, const int baud)
{
struct termios options ;
speed_t myBaud ;
int status, fd ;
switch (baud){
case 9600: myBaud = B9600 ; break ;
case 115200: myBaud = B115200 ; break ;
}
if ((fd = open (device, O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK)) == -1)
return -1 ;
fcntl (fd, F_SETFL, O_RDWR) ;
// Get and modify current options:
tcgetattr (fd, &options) ;
cfmakeraw (&options) ;
cfsetispeed (&options, myBaud) ;
cfsetospeed (&options, myBaud) ;
options.c_cflag |= (CLOCAL | CREAD) ;
options.c_cflag &= ~PARENB ;
options.c_cflag &= ~CSTOPB ;
options.c_cflag &= ~CSIZE ;
options.c_cflag |= CS8 ;
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG) ;
options.c_oflag &= ~OPOST ;
options.c_cc [VMIN] = 0 ;
options.c_cc [VTIME] = 100 ; // Ten seconds (100 deciseconds)
tcsetattr (fd, TCSANOW, &options) ;
ioctl (fd, TIOCMGET, &status);
status |= TIOCM_DTR ;
status |= TIOCM_RTS ;
ioctl (fd, TIOCMSET, &status);
usleep (10000) ; // 10mS
return fd ;
}
//0xAA 0x55 0x64 00 0x55 0xAA //0x AA > 157 unsigned
void serialSendstring (const int fd, const unsigned char *s,int len)
{
int ret;
ret = write (fd, s, len); //我们有结束位,而且不为'\0',所以不能用strlen
if (ret < 0)
printf("Serial Puts Error\n");
}
int serialGetstring (const int fd,unsigned char *buffer)
{
int n_read;
n_read = read(fd, buffer,32);
return n_read;
}