Advertisement
Kosty_Fomin

Untitled

Aug 23rd, 2016
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.89 KB | None | 0 0
  1. from __future__ import print_function
  2. from Tkinter import *
  3. import Tkinter as tk
  4. import cv2
  5. from Authorization import auth
  6. from PIL import Image, ImageTk
  7. import cv2.cv as cv
  8. import pdb
  9. import httplib, urllib, urllib2
  10. import json
  11. from datetime import datetime
  12. import sys, pygn, json
  13. import string
  14. from random import randint
  15. import shutil
  16.  
  17. class Frame(object):
  18.  
  19.     def __init__(self, master):
  20.         self.master = master
  21.  
  22.         master.wm_title("")
  23.         master.wm_protocol("WM_DELETE_WINDOW")
  24.         self.cap = cv2.VideoCapture(0)
  25.  
  26.         self.b = Button(width = 25, height = 8, text = 'Music!',command = self.start)
  27.         self.b.place(x = 30, y = 20)
  28.  
  29.         self.t = Text(font=('Helvetica',24),width=25,height=12,wrap=WORD)
  30.         self.t.configure(state="disabled")
  31.         self.t.pack(side = LEFT)
  32.  
  33.         self.tv = Text(font=('Helvetica',24),width=20,height=3,wrap=WORD)
  34.         self.tv.configure(state="normal",background='grey')
  35.         self.tv.insert(1.0, "")
  36.         self.tv.place(x = 30, y = 500)
  37.  
  38.         self.bv = Button(width = 25, height = 8, text = 'Search!',command = self.setVkMusic)
  39.         self.bv.place(x = 30, y = 600)
  40.  
  41.         self.lmain = tk.Label(master)
  42.         self.lmain.pack(side = RIGHT)
  43.  
  44.         self.savingImage = None    
  45.  
  46.         self.r = ""
  47.        
  48.  
  49.     def show_frame(self):
  50.         _, frame = self.cap.read()
  51.         frame = cv2.flip(frame, 1)
  52.         self.savingImage = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA)
  53.         img = Image.fromarray(self.savingImage)
  54.         self.imgtk = ImageTk.PhotoImage(image=img)
  55.         self.lmain.imgtk = self.imgtk
  56.         self.lmain.configure(image=self.imgtk)
  57.         self.lmain.after(50, self.show_frame)
  58.  
  59.     def takePhoto(self,s):
  60.         ramp_frames = 30
  61.         self.camera = cv2.VideoCapture(0)
  62.         def get_image():
  63.             retval, im = self.camera.read()
  64.             return im
  65.          
  66.         for i in xrange(ramp_frames):
  67.             temp = get_image()
  68.         camera_capture = get_image()
  69.         cv2.imwrite(s, camera_capture)
  70.         del(self.camera)
  71.  
  72.     def start(self):
  73.         headers = {
  74.             # Request headers
  75.             'Content-Type': 'application/octet-stream',
  76.             'Ocp-Apim-Subscription-Key': 'faa920d267974c84a43a260a51454b7e',
  77.         }
  78.  
  79.         try:
  80.             s ="images/{}.png".format("{:%B_%d_%Y_%H:%M:%S}".format(datetime.now()))
  81.             self.takePhoto(s)
  82.             with open(s, "rb") as imageFile:
  83.                 d = imageFile.read()
  84.  
  85.             conn = httplib.HTTPSConnection('api.projectoxford.ai')
  86.             conn.request("POST", "https://api.projectoxford.ai/emotion/v1.0/recognize", d, headers)
  87.             response = conn.getresponse()
  88.             data = response.read()
  89.  
  90.             dat = json.loads(data)
  91.  
  92.  
  93.             int_emotions = 0
  94.             str_emotions = ""
  95.  
  96.             self.r = ""
  97.             self.t.configure(state="normal")
  98.             self.t.delete('1.0', END)
  99.  
  100.             for keys,values in dat[0]['scores'].items():
  101.                 self.r += keys
  102.                 self.r += " " + str(int(values*100))+ "\n"
  103.                 print(self.r)
  104.                 if int(values*100) > int_emotions:
  105.                     int_emotions = int(values*100)
  106.                     str_emotions = keys
  107.  
  108.             self.t.insert(1.0, self.r)
  109.             print(self.r)
  110.             self.t.configure(state="disabled")
  111.             conn.close()
  112.  
  113.             s = str_emotions+"/"+ str(randint(1,4)) +".mp3"
  114.             print(s)
  115.             shutil.move(s, "/Volumes/one_direction/0.mp3")
  116.             #self.setVkMusic()
  117.         except Exception as e:
  118.             print("{}".format(e.message))
  119.  
  120.     def setVkMusic(self):
  121.         """LOGIN = ("+")
  122.         PASSWORD = ("")
  123.         CLIENT = ("4949157")
  124.         SCOPE = '341055'
  125.         token, user_id = auth(LOGIN, PASSWORD, CLIENT, SCOPE)"""
  126.         ZAEBALO = self.tv.get("1.0",END)
  127.         ZAEBALO = ZAEBALO.replace(" ", "_")
  128.         ZAEBALO = ZAEBALO.replace("\n", "")
  129.         print(ZAEBALO)
  130.         token = 'f0c0eb66a45941864aae34fd62e21f46cdfcb67e47bef0658cb3a118d0817f040ba9d3c18790bcd0908d0'
  131.         s = "https://api.vk.com/method/audio.search?q={0}&sort=2&count=1&access_token={1}".format(ZAEBALO,token)
  132.         #print(s)
  133.         h = json.loads(urllib2.urlopen(s).read())
  134.         #print (h['response'][1]['url'].split('?')[0])
  135.         print("Done")
  136.         mp3 = h['response'][1]['url'].split('?')[0]
  137.         mp3file = urllib2.urlopen(mp3)
  138.         with open('/Volumes/one_direction/0.mp3','wb') as output:
  139.             output.write(mp3file.read())
  140.         output.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement