Advertisement
Guest User

Untitled

a guest
May 6th, 2017
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.39 KB | None | 0 0
  1. from watson_developer_cloud import SpeechToTextV1
  2. import json, base64
  3. from nltk.tokenize import word_tokenize
  4. import re
  5. import matplotlib.pyplot as plt
  6. from watson_developer_cloud import NaturalLanguageUnderstandingV1
  7. import watson_developer_cloud.natural_language_understanding.features.v1 as \
  8. features
  9. from wordcloud import WordCloud
  10. import os
  11. import subprocess
  12. ##removes non non-alphanumeric characters based on re
  13. def re_nalpha(str):
  14. pattern = re.compile(r'[^\w\s]', re.U)
  15. return re.sub(r'_', '', re.sub(pattern, '', str))
  16.  
  17. def wfreq (text):
  18. word_dic = {}
  19. if isinstance(text,str):
  20. #preprocessing module is used to tokenize the text
  21. text = word_tokenize(re_nalpha(text))
  22. for word in text:
  23. if word not in word_dic:
  24. word_dic[word] = 1
  25. else:
  26. word_dic[word] +=1
  27. else:
  28. for sent in text:
  29. sent_str = word_tokenize(re_nalpha(sent[0]))
  30. for word in sent_str:
  31. if word not in word_dic:
  32. word_dic[word] = 1
  33. else:
  34. word_dic[word] +=1
  35. sorted_list = sorted(word_dic.items(), key=lambda d: d[1],reverse=True)
  36. return sorted_list
  37.  
  38. def create_pic(input_text):
  39. sorted_list=wfreq(input_text)
  40. print (sorted_list)
  41. x_list = []
  42. y_list = []
  43. for item in sorted_list:
  44. x_list.append(item[0])
  45. y_list.append(item[1])
  46. x=range(len(x_list[:5]))
  47. plt.bar(x, y_list[:5],width=0.9)
  48. plt.xticks((x),x_list)
  49. plt.savefig('Interviewer/static/Interviewer/userMedia/freq.png')
  50. plt.close()
  51.  
  52. def create_emo(input_text):
  53. natural_language_understanding = NaturalLanguageUnderstandingV1(
  54. version='2017-02-27',
  55. username='6a28fa30-2bd7-4981-9037-09e3e8a3a691',
  56. password='ZtUrPvaSmxKL')
  57.  
  58. response = natural_language_understanding.analyze(
  59. text=input_text,
  60. features=[features.Emotion()])
  61. result=json.loads(json.dumps(response, indent=2))
  62. print(result)
  63. emo_rate=result["emotion"]["document"]["emotion"]
  64. labels = ['sadness', 'joy', 'fear', 'disgust','anger']
  65. sizes = [emo_rate['sadness'], emo_rate['joy'], emo_rate['fear'], emo_rate['disgust'],emo_rate['anger']]
  66. colors = ['yellowgreen', 'gold', 'lightskyblue', 'lightcoral','black']
  67. patches, texts = plt.pie(sizes, colors=colors, shadow=True, startangle=90)
  68. plt.legend(patches, labels, loc="best")
  69. plt.tight_layout()
  70. plt.savefig('Interviewer/static/Interviewer/userMedia/emo.png')
  71. plt.close()
  72. def word_could(text):
  73. # Generate a word cloud image
  74. wordcloud = WordCloud().generate(text)
  75. wordcloud = WordCloud(max_font_size=40).generate(text)
  76. plt.figure()
  77. plt.imshow(wordcloud, interpolation="bilinear")
  78. plt.axis("off")
  79. plt.savefig('Interviewer/static/Interviewer/userMedia/wordcloud.png')
  80. plt.close()
  81.  
  82. def pic_anly():
  83. p = subprocess.Popen('curl -X POST "https://v1-api.visioncloudapi.com/face/detection?api_id=5a0f0ffc899a4db88241a0e7bd4ed306&api_secret=f66a525a98e54c3e97bb1d6ee9d6d4c7&attributes=true" \
  84. -F file=@user_photo.png', stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd='C:\\Users\\rabbani\\PycharmProjects\\Interview-AI')
  85. out, error = p.communicate()
  86. result=json.loads(out.decode("utf-8"))
  87. desc = ''
  88. if(len(result['faces'])==1):
  89. att=result['faces'][0]['attributes']
  90. emo=result['faces'][0]['emotions']
  91. desc+="Description for the appearance" + '\n'
  92. desc+="age: "+str(att['age'])+'\n'
  93. d_smile='No'
  94. if(att['smile']==1):
  95. d_smile='yes'
  96. desc+="smile: "+d_smile+'\n'
  97. gender = att['gender']
  98. d_gender=''
  99. if(gender>50):
  100. d_gender='Male'
  101. else:
  102. d_gender='Female'
  103. desc+="Gender: " + d_gender + '\n'
  104.  
  105. glass = att['eyeglass']
  106. d_glass = ''
  107.  
  108. if(glass>50):
  109. d_glass="with glass"
  110. else:
  111. d_glass="without glass"
  112. desc+="Eyeglass: " + d_glass + '\n'
  113. desc+="\nDescription for the emotion:" + '\n'
  114. d_emo=''
  115. emo_list = ["angry","calm","confused","disgust","happy","sad","scared","surprised","screaming"]
  116. for sub_emo in emo_list:
  117. if(emo[sub_emo]==1):
  118. d_emo+=sub_emo
  119. desc+=d_emo+'\n'
  120. else:
  121. desc="Invalid picture - mutiple faces detected or nofaces detected"
  122. return (desc)
  123.  
  124. def speech_to_text(b64code):
  125.  
  126. stt = SpeechToTextV1(username="5ff4851b-de60-45c5-9fdb-e0d7d9b866c2", password="3siYdkUyzVoj")
  127.  
  128. audio_file = base64.b64decode(b64code)
  129.  
  130. result = json.dumps(stt.recognize(audio_file, content_type="audio/wav",continuous="true",timestamps="true",word_confidence="True",model="en-UK_NarrowbandModel"), indent=2)
  131.  
  132. # print result
  133. wjdata = json.loads(result)
  134.  
  135. temp_str = ''
  136. for subs in wjdata["results"]:
  137. # print "transcript"
  138. temp_str+=subs["alternatives"][0]["transcript"]
  139. print(temp_str)
  140. timestamps = subs["alternatives"][0]["timestamps"]
  141. average_velocity = round(60/((timestamps[-1][2]-timestamps[0][1])/len(timestamps)))
  142. print(average_velocity)
  143. create_pic(temp_str)
  144. create_emo(temp_str)
  145. word_could(temp_str)
  146. result = {'speech_str': temp_str, 'average_velocity': average_velocity}
  147. print(pic_anly())
  148. return result
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement