├── .gitignore ├── LICENSE ├── README.md ├── curl.R ├── food.csv ├── gene.csv ├── genefan_logo.png ├── history.csv ├── requirements.txt ├── segmentfault-hackathon-2015.Rproj ├── server.R ├── ui.R ├── user.csv ├── www ├── bocai.jpg ├── chunniunai.jpg ├── gene-logo.ai ├── pijiu.jpg ├── zhishi.jpg └── 测试图片.zip ├── 爬虫.R └── 画圆.R /.gitignore: -------------------------------------------------------------------------------- 1 | .Rproj.user 2 | .Rhistory 3 | .RData 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 HarryZhu 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # segmentfault-hackathon-2015 2 | 3 | 本项目于2015-10-24 到 2015-10-25截止,利用shiny+docker+ggplot2+RCurl技术,破解了百度识图API,并利用shiny构建了前后端分离的R语言Web开发框架,实践了敏捷开发。 4 | 本项目致力于解决人体基因与膳食解决方案,基于人体基因的某些天然缺陷和食品成分,我们提供了完整的膳食分析和推荐技术,为用户解决科学膳食的问题,母婴、老年用户将成为我们的主力用户。 5 | 本次大赛该项目有幸获得了特别奖,键盘为PORK键盘一个。 6 | 7 | 技术实现比较粗糙,大家见谅。 8 | 9 | 需要安装一些依赖包: 10 | ``` 11 | install.packages("shiny",repos="http://mirror.bjtu.edu.cn/cran/") 12 | install.packages("shinydashboard",repos="http://mirror.bjtu.edu.cn/cran/") 13 | install.packages("plotrix",repos="http://mirror.bjtu.edu.cn/cran/") 14 | install.packages("ggplot2",repos="http://mirror.bjtu.edu.cn/cran/") 15 | install.packages("RCurl",repos="http://mirror.bjtu.edu.cn/cran/") 16 | # 安装XML依赖 17 | system("sudo apt-get install libxml2-dev") # ubuntu 18 | system("sudo yum install libxml2-devel")# CentOS/RHEL 19 | install.packages("XML",repos="http://mirror.bjtu.edu.cn/cran/") 20 | install.packages("jpeg",repos="http://mirror.bjtu.edu.cn/cran/") 21 | ``` 22 | 23 | ``` 24 | # 运行 25 | runApp() 26 | ``` 27 | -------------------------------------------------------------------------------- /curl.R: -------------------------------------------------------------------------------- 1 | library(jsonlite) 2 | library(XML) 3 | library(RCurl) 4 | 5 | myHttpheader<- c( 6 | "User-Agent"="Mozilla/5.0(Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.6)", 7 | "Accept"="text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", 8 | "Accept-Language"="en-us", 9 | "Connection"="keep-alive", 10 | "Accept-Charset"="GB2312,utf-8;q=0.7,*;q=0.7" 11 | ) 12 | 13 | food_recognition<-function(img_id){ 14 | curl_url=paste("http://stu.baidu.com/n/pc_search?rn=10&appid=0&tag=1&isMobile=0&queryImageUrl=http://7xnf88.com1.z0.glb.clouddn.com/",img_id,"&querySign=&fromProduct=&productBackUrl=&fm=&uptype=plug_in",sep="") 15 | v<- getURL(curl_url,httpheader=myHttpheader,.encoding='gbk') 16 | x<-regmatches(v, regexpr('', v)) 17 | doc = htmlParse(x, asText=TRUE,encoding='utf-8') 18 | xpathSApply(doc, "//text()",xmlValue) 19 | } 20 | img_id = 'chunniunai.jpg' 21 | food_recognition(img_id) 22 | 23 | -------------------------------------------------------------------------------- /food.csv: -------------------------------------------------------------------------------- 1 | username,timestamp,upload,food 2 | minqingjie1,1445664439,/tmp/1111.jpg,cheese 3 | minqingjie1,1445668039,/tmp/2222.jpg,beer 4 | minqingjie1,1445670039,/tmp/3333.jpg,spinach 5 | minqingjie1,1445672039,/tmp/4444.jpg,milk 6 | -------------------------------------------------------------------------------- /gene.csv: -------------------------------------------------------------------------------- 1 | food,component,score 2 | cheese,luoan,63 3 | beer,yichun,70 4 | spinach,yesuan,90 5 | milk,lutang,70 6 | -------------------------------------------------------------------------------- /genefan_logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/harryprince/segmentfault-hackathon-2015/fc31dcfe5060ab9c71b114ff323b4f32b5872e5a/genefan_logo.png -------------------------------------------------------------------------------- /history.csv: -------------------------------------------------------------------------------- 1 | username,food,score,timestamp 2 | minqingjie1,spinach,41,1 3 | minqingjie1,milk,85,2 4 | minqingjie1,spinach,43,3 5 | minqingjie1,milk,82,4 6 | minqingjie1,cheese,20,5 7 | minqingjie1,milk,90,6 8 | minqingjie1,beer,44,7 9 | minqingjie1,milk,93,8 10 | minqingjie1,spinach,40,9 11 | minqingjie1,cheese,21,10 12 | minqingjie1,milk,92,11 13 | minqingjie1,milk,54,12 14 | minqingjie1,beer,40,13 15 | minqingjie1,milk,75,14 16 | minqingjie1,cheese,20,15 17 | minqingjie1,milk,93,16 18 | minqingjie1,spinach,40,17 19 | minqingjie1,milk,74,18 20 | minqingjie1,milk,82,19 21 | minqingjie1,beer,40,20 22 | minqingjie1,milk,80,21 23 | minqingjie1,cheese,63,22 24 | minqingjie1,beer,90,23 25 | minqingjie1,spinach,48,24 26 | minqingjie1,beer,42,25 27 | minqingjie1,milk,74,26 28 | minqingjie1,beer,65,27 29 | minqingjie1,beer,44,28 30 | minqingjie1,milk,71,29 31 | minqingjie1,cheese,42,30 32 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/harryprince/segmentfault-hackathon-2015/fc31dcfe5060ab9c71b114ff323b4f32b5872e5a/requirements.txt -------------------------------------------------------------------------------- /segmentfault-hackathon-2015.Rproj: -------------------------------------------------------------------------------- 1 | Version: 1.0 2 | 3 | RestoreWorkspace: Default 4 | SaveWorkspace: Default 5 | AlwaysSaveHistory: Default 6 | 7 | EnableCodeIndexing: Yes 8 | UseSpacesForTab: Yes 9 | NumSpacesForTab: 2 10 | Encoding: UTF-8 11 | 12 | RnwWeave: Sweave 13 | LaTeX: pdfLaTeX 14 | -------------------------------------------------------------------------------- /server.R: -------------------------------------------------------------------------------- 1 | # This is the server logic for a Shiny web application. 2 | # You can find out more about building applications with Shiny here: 3 | # 4 | # http://www.rstudio.com/shiny/ 5 | # 6 | 7 | library(shiny) 8 | library(shinydashboard) 9 | #library(grid) 10 | library(plotrix) 11 | library(ggplot2) 12 | library(httr) 13 | library(RCurl) 14 | library(jsonlite) 15 | library(XML) 16 | library(jpeg) 17 | 18 | # 百度识图破解版API 19 | 20 | food_recognition<-function(img_id){ 21 | myHttpheader<- c( 22 | "User-Agent"="Mozilla/5.0(Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.6)", 23 | "Accept"="text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", 24 | "Accept-Language"="en-us", 25 | "Connection"="keep-alive", 26 | "Accept-Charset"="GB2312,utf-8;q=0.7,*;q=0.7" 27 | ) 28 | curl_url=paste("http://stu.baidu.com/n/pc_search?rn=10&appid=0&tag=1&isMobile=0&queryImageUrl=http://7xnf88.com1.z0.glb.clouddn.com/",img_id,"&querySign=&fromProduct=&productBackUrl=&fm=&uptype=plug_in",sep="") 29 | 30 | v<- getURL(curl_url,httpheader=myHttpheader,.encoding='gbk') 31 | 32 | x<-regmatches(v, regexpr('', v)) 33 | doc = htmlParse(x, asText=TRUE,encoding='utf-8') 34 | xpathSApply(doc, "//text()",xmlValue) 35 | } 36 | 37 | 38 | shinyServer( 39 | function(input, output) { 40 | # if(exists(input$file)){POST} 41 | #logo 42 | # is_input_file<-reactive({is.null(input$file)}) 43 | 44 | output$logo<-renderUI({ 45 | img(src="http://7xnf88.com1.z0.glb.clouddn.com/genefan_logo.png",align='center',width = "288px", height = "144px") 46 | }) 47 | #显示图片 48 | output$image_status<-renderText({ 49 | if ({is.null(input$file)}) 50 | return(NULL) 51 | c('你的美食:') 52 | }) 53 | #显示图片 54 | output$user_upload <- renderUI({ 55 | if ({is.null(input$file)}) 56 | return(NULL) 57 | img_id = input$file["name"] 58 | curl_url = paste("http://7xnf88.com1.z0.glb.clouddn.com/",img_id,sep="") 59 | img(src = curl_url,width='200px') 60 | 61 | 62 | }) 63 | 64 | 65 | # read.csv(inFile$datapath, header=input$header, sep=input$sep, quote=input$quote) 66 | 67 | # image 68 | output$food_name <- renderText(function() { 69 | if (is.null(input$file)) { 70 | # User has not uploaded a file yet 71 | return(NULL) 72 | } 73 | tryCatch({ 74 | food_recognition(input$file["name"]) 75 | }, 76 | error = function(err){ 77 | return("未知食物") 78 | }) 79 | 80 | }) 81 | 82 | 83 | #显示标题 84 | output$index<-renderText({ 85 | if ({is.null(input$file)}) 86 | return(NULL) 87 | c('你的基因美食匹配度') 88 | 89 | 90 | }) 91 | # 得分 92 | output$circle<- renderPlot({ 93 | 94 | inFile <- input$file 95 | 96 | if (is.null(inFile)) 97 | return(NULL) 98 | 99 | library(plotrix) 100 | 101 | if (food_recognition(input$file["name"]) == "纯牛奶"){a=80;color='gray';advice=c('纯牛奶的钙磷比例适当,利于钙的吸收,但MCM6基因产生突变,导致乳糖代谢能力差,易出现腹泻、腹胀或腹绞痛等症状。需降低牛奶的饮用量,建议你喝低乳糖牛奶或者酸奶.')} 102 | else{ 103 | if (food_recognition(input$file["name"]) == "菠菜"){a=70;color='green';advice=c('菠菜富含铁元素,有利于预防贫血,但菠菜中的胡萝卜素可能会导致您身体过敏,应选择其他蔬菜')} 104 | else{ 105 | if(food_recognition(input$file["name"]) == "美酒"){a=20;color="yellow";advice=c('啤酒中的硅会导致您身体不适,我们建议您多喝蔬菜汁,代替啤酒。')} 106 | else{ 107 | if(food_recognition(input$file["name"]) == "芝士"){a=40;color='blue';advice=c("适量吃奶酪是很好的,奶酪是钙的很好的来源,但吃多了不容易消化,不适合肠胃不好的人")} 108 | else{a=15;color='purple';advice=c("暂无建议")} 109 | } 110 | } 111 | } 112 | 113 | 114 | 115 | par(family='STKaiti',mar=c(0,0,0,0)) 116 | plot(1:10,seq(1,10,length=10),type="n",axes=F,xlab="",ylab="",frame.plot=F)#,main='本餐基因饮食匹配度') 117 | draw.circle(5,5,c(2,2,2),border=color, col=color,lty=1,lwd=1) 118 | draw.circle(5,5,1.5,border="blue",lty=2,lwd=2) 119 | text.default(5,5,labels=a,cex=7)# 120 | }) 121 | # 建议 122 | output$description<-renderText({ 123 | if (is.null(input$file)) 124 | return(NULL) 125 | c('建议') 126 | 127 | }) 128 | 129 | # 建议 130 | output$suggestion<-renderText({ 131 | if (is.null(input$file)) 132 | return(NULL) 133 | 134 | if (food_recognition(input$file["name"]) == "纯牛奶"){a=80;color='gray';advice=c('纯牛奶的钙磷比例适当,利于钙的吸收,但MCM6基因产生突变,导致乳糖代谢能力差,易出现腹泻、腹胀或腹绞痛等症状。需降低牛奶的饮用量,建议你喝低乳糖牛奶或者酸奶.')} 135 | else{ 136 | if (food_recognition(input$file["name"]) == "菠菜"){a=70;color='green';advice=c('菠菜富含铁元素,有利于预防贫血,但菠菜中的胡萝卜素可能会导致您身体过敏,应选择其他蔬菜')} 137 | else{ 138 | if(food_recognition(input$file["name"]) == "美酒"){a=20;color="yellow";advice=c('啤酒中的硅会导致您身体不适,我们建议您多喝蔬菜汁,代替啤酒。')} 139 | else{ 140 | if(food_recognition(input$file["name"]) == "芝士"){a=40;color='blue';advice=c("适量吃奶酪是很好的,奶酪是钙的很好的来源,但吃多了不容易消化,不适合肠胃不好的人")} 141 | else{a=15;color='purple';advice=c("暂无建议")} 142 | } 143 | } 144 | } 145 | advice 146 | 147 | }) 148 | output$history<- renderPlot({ 149 | temp= input$date 150 | # typeof(temp); 151 | # length(temp); 152 | library(plotrix) 153 | par(family='STKaiti',mar=c(0,0,0,0)) 154 | history = read.csv("history.csv") 155 | sunspotyear1 <- data.frame(y=history$score[1:temp],x= history$timestamp[1:temp]) 156 | input$date["value"] 157 | p=ggplot(sunspotyear1, aes(x=x, y=y)) + geom_point(col='green',alpha=.3,size=5)+geom_point(col='green',alpha=.7)+geom_area(fill="green",alpha=.1) + geom_line(col='green',alpha=0.5)+scale_x_discrete(breaks=sunspotyear1$x[1:temp],labels=history$timestamp[1:temp]) + xlab("") 158 | 159 | 160 | p+ylab("")+theme(axis.title.y=element_text(angle=0)) 161 | 162 | 163 | 164 | }) 165 | } 166 | ) -------------------------------------------------------------------------------- /ui.R: -------------------------------------------------------------------------------- 1 | # This is the user-interface definition of a Shiny web application. 2 | # You can find out more about building applications with Shiny here: 3 | # 4 | # http://www.rstudio.com/shiny/ 5 | # 6 | 7 | library(shiny) 8 | 9 | shinyUI(fluidPage( 10 | headerPanel("我的基因,我的饭!"), 11 | sidebarLayout( 12 | # Application title 13 | # Sidebar with a slider input for number of bins 14 | sidebarPanel( 15 | fileInput(inputId = "file",label = "开始匹配:", multiple = 1, accept = c( 16 | 'image/jpeg', 17 | 'image/jpg', 18 | 'image/png', 19 | 'image/gif')) 20 | #, 21 | #datapath='temp') 22 | # ), 23 | # fileInput("files", "File data", multiple=TRUE) 24 | ), 25 | 26 | # actionButton("match","开始匹配") 27 | 28 | 29 | # Show a plot of the generated distribution 30 | mainPanel( 31 | uiOutput('logo'), 32 | h2(textOutput("image_status")), 33 | uiOutput('user_upload'), 34 | h3(textOutput("food_name"),align='center'), 35 | h1(textOutput("index"),align='center'), 36 | plotOutput("circle"), 37 | # tabPanel("Live Images", 38 | # conditionalPanel(condition = "input.image_type == 'img_type1'", 39 | # img(src = "img_type1.jpeg") 40 | # ), 41 | # conditionalPanel(condition = "input.image_type == 'img_type2'", 42 | # img(src = "img_type2.jpeg") 43 | # ) 44 | # ), 45 | # 46 | h2(textOutput("description")), 47 | h2(" ",margin=c(2,0,0,0)), 48 | h5(textOutput("suggestion"),align='center'), 49 | h2("基因检测结果",align='center'), 50 | h3("MCM6 基因",align='center',col="red"), 51 | h3("rs4988235 GG低活性",align='center'), 52 | h3("rs182549 CC低活性",align='center'), 53 | h2("每日健康值",align='center'), 54 | plotOutput("history"), 55 | sliderInput('date',min=1,max=25,step=1,value=10,label='时段选取',animate=T), 56 | h2("日期",align='center') 57 | ) 58 | ) 59 | ) 60 | ) -------------------------------------------------------------------------------- /user.csv: -------------------------------------------------------------------------------- 1 | username,password,logintime,loginip,mail,phone,address,gender 2 | minqingjie1,11111111,1425870961,123.125.144.70,minqingjie1@yeah.net,15611116454,北京朝阳,男 3 | minqingjie2,11111112,1425870962,123.125.144.71,minqingjie2@yeah.net,15611116454,北京朝阳,男 4 | minqingjie3,11111113,1425870963,123.125.144.72,minqingjie3@yeah.net,15611116454,北京朝阳,男 5 | minqingjie4,11111114,1425870964,123.125.144.73,minqingjie4@yeah.net,15611116454,北京朝阳,男 6 | minqingjie5,11111115,1425870965,123.125.144.74,minqingjie5@yeah.net,15611116454,北京朝阳,男 7 | minqingjie6,11111116,1425870966,123.125.144.75,minqingjie6@yeah.net,15611116454,北京朝阳,男 8 | minqingjie7,11111117,1425870967,123.125.144.76,minqingjie7@yeah.net,15611116454,北京朝阳,男 9 | minqingjie8,11111118,1425870968,123.125.144.77,minqingjie8@yeah.net,15611116454,北京朝阳,男 10 | minqingjie9,11111119,1425870969,123.125.144.78,minqingjie9@yeah.net,15611116454,北京朝阳,男 11 | -------------------------------------------------------------------------------- /www/bocai.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/harryprince/segmentfault-hackathon-2015/fc31dcfe5060ab9c71b114ff323b4f32b5872e5a/www/bocai.jpg -------------------------------------------------------------------------------- /www/chunniunai.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/harryprince/segmentfault-hackathon-2015/fc31dcfe5060ab9c71b114ff323b4f32b5872e5a/www/chunniunai.jpg -------------------------------------------------------------------------------- /www/gene-logo.ai: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/harryprince/segmentfault-hackathon-2015/fc31dcfe5060ab9c71b114ff323b4f32b5872e5a/www/gene-logo.ai -------------------------------------------------------------------------------- /www/pijiu.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/harryprince/segmentfault-hackathon-2015/fc31dcfe5060ab9c71b114ff323b4f32b5872e5a/www/pijiu.jpg -------------------------------------------------------------------------------- /www/zhishi.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/harryprince/segmentfault-hackathon-2015/fc31dcfe5060ab9c71b114ff323b4f32b5872e5a/www/zhishi.jpg -------------------------------------------------------------------------------- /www/测试图片.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/harryprince/segmentfault-hackathon-2015/fc31dcfe5060ab9c71b114ff323b4f32b5872e5a/www/测试图片.zip -------------------------------------------------------------------------------- /爬虫.R: -------------------------------------------------------------------------------- 1 | library(jsonlite) 2 | library(XML) 3 | library(RCurl) 4 | 5 | myHttpheader<- c( 6 | "User-Agent"="Mozilla/5.0(Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.6)", 7 | "Accept"="text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", 8 | "Accept-Language"="en-us", 9 | "Connection"="keep-alive", 10 | "Accept-Charset"="GB2312,utf-8;q=0.7,*;q=0.7" 11 | ) 12 | 13 | food_recognition<-function(img_id){ 14 | curl_url=paste("http://stu.baidu.com/n/pc_search?rn=10&appid=0&tag=1&isMobile=0&queryImageUrl=http://7xnf88.com1.z0.glb.clouddn.com/",img_id,"&querySign=&fromProduct=&productBackUrl=&fm=&uptype=plug_in",sep="") 15 | v<- getURL(curl_url,httpheader=myHttpheader,.encoding='gbk') 16 | x<-regmatches(v, regexpr('', v)) 17 | doc = htmlParse(x, asText=TRUE,encoding='utf-8') 18 | xpathSApply(doc, "//text()",xmlValue) 19 | } 20 | img_id = 'chunniunai.jpg' 21 | food_recognition(img_id) 22 | -------------------------------------------------------------------------------- /画圆.R: -------------------------------------------------------------------------------- 1 | # 画圆 2 | library("plotrix") 3 | par(family='STKaiti',mar=c(0,0,0,0)) 4 | plot(1:10,seq(1,10,length=10),type="n",axes=F,xlab="",ylab="",frame.plot=F)#,main="烧豆腐") 5 | draw.circle(5,5,c(2,2,2),border="purple", col=c("#ff00ff","#ff77ff","#ffccff"),lty=1,lwd=1) 6 | text.default(5,5,labels="80",cex=7) 7 | 8 | --------------------------------------------------------------------------------