pythonreadhdf5(好玩的电脑小代码)

1.好玩的电脑小代码

#coding=utf-8

#表情识别

import cv2

from keras.models import load_model

import numpy as np

import chineseText

import datetime

startTime = datetime.datetime.now()

emotion_classifier = load_model(

'classifier/emotion_models/simple_CNN.530-0.65.hdf5')

endTime = datetime.datetime.now()

print(endTime - startTime)

emotion_labels = {

0: '生气',

1: '厌恶',

2: '恐惧',

3: '开心',

4: '难过',

5: '惊喜',

6: '平静'

}

img = cv2.imread("img/emotion/emotion.png")

face_classifier = cv2.CascadeClassifier(

"C:\Python36\Lib\site-packages\opencv-master\data\haarcascades\haarcascade_frontalface_default.xml"

)

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

faces = face_classifier.detectMultiScale(

gray, scaleFactor=1.2, minNeighbors=3, minSize=(40, 40))

color = (255, 0, 0)

for (x, y, w, h) in faces:

gray_face = gray[(y):(y + h), (x):(x + w)]

gray_face = cv2.resize(gray_face, (48, 48))

gray_face = gray_face / 255.0

gray_face = np.expand_dims(gray_face, 0)

gray_face = np.expand_dims(gray_face, -1)

emotion_label_arg = np.argmax(emotion_classifier.predict(gray_face))

emotion = emotion_labels[emotion_label_arg]

cv2.rectangle(img, (x + 10, y + 10), (x + h - 10, y + w - 10),

(255, 255, 255), 2)

img = chineseText.cv2ImgAddText(img, emotion, x + h * 0.3, y, color, 20)

cv2.imshow("Image", img)

cv2.waitKey(0)

cv2.destroyAllWindows()

2.python中输入content=urllib.urlopen(url).read(),按F5运行后没有输

#coding:utf-8import urllib.requeststr0='一次告别'title=str0.find(r'。

pythonreadhdf5

3.python rbf表示什么分布

径向基(RBF)神经网络python实现 1 from numpy import array, append, vstack, transpose, reshape, \ 2 dot, true_divide, mean, exp, sqrt, log, \ 3 loadtxt, savetxt, zeros, frombuffer 4 from numpy.linalg import norm, lstsq 5 from multiprocessing import Process, Array 6 from random import sample 7 from time import time 8 from sys import stdout 9 from ctypes import c_double 10 from h5py import File 11 12 13 def metrics(a, b): 14 return norm(a - b) 15 16 17 def gaussian (x, mu, sigma): 18 return exp(- metrics(mu, x)**2 / (2 * sigma**2)) 21 def multiQuadric (x, mu, sigma): 22 return pow(metrics(mu,x)**2 + sigma**2, 0.5) 23 24 25 def invMultiQuadric (x, mu, sigma): 26 return pow(metrics(mu,x)**2 + sigma**2, -0.5) 27 28 29 def plateSpine (x,mu): 30 r = metrics(mu,x) 31 return (r**2) * log(r) 32 33 34 class Rbf: 35 def __init__(self, prefix = 'rbf', workers = 4, extra_neurons = 0, from_files = None): 36 self.prefix = prefix 37 self.workers = workers 38 self.extra_neurons = extra_neurons 39 40 # Import partial model 41 if from_files is not None: 42 w_handle = self.w_handle = File(from_files['w'], 'r') 43 mu_handle = self.mu_handle = File(from_files['mu'], 'r') 44 sigma_handle = self.sigma_handle = File(from_files['sigma'], 'r') 45 46 self.w = w_handle['w'] 47 self.mu = mu_handle['mu'] 48 self.sigmas = sigma_handle['sigmas'] 49 50 self.neurons = self.sigmas.shape[0] 51 52 def _calculate_error(self, y): 53 self.error = mean(abs(self.os - y)) 54 self.relative_error = true_divide(self.error, mean(y)) 55 56 def _generate_mu(self, x): 57 n = self.n 58 extra_neurons = self.extra_neurons 59 60 # TODO: Make reusable 61 mu_clusters = loadtxt('clusters100.txt', delimiter='\t') 62 63 mu_indices = sample(range(n), extra_neurons) 64 mu_new = x[mu_indices, :] 65 mu = vstack((mu_clusters, mu_new)) 66 67 return mu 68 69 def _calculate_sigmas(self): 70 neurons = self.neurons 71 mu = self.mu 72 73 sigmas = zeros((neurons, )) 74 for i in xrange(neurons): 75 dists = [0 for _ in xrange(neurons)] 76 for j in xrange(neurons): 77 if i != j: 78 dists[j] = metrics(mu[i], mu[j]) 79 sigmas[i] = mean(dists)* 2 80 # max(dists) / sqrt(neurons * 2)) 81 return sigmas 82 83 def _calculate_phi(self, x): 84 C = self.workers 85 neurons = self.neurons 86 mu = self.mu 87 sigmas = self.sigmas 88 phi = self.phi = None 89 n = self.n 90 91 92 def heavy_lifting(c, phi): 93 s = jobs[c][1] - jobs[c][0] 94 for k, i in enumerate(xrange(jobs[c][0], jobs[c][1])): 95 for j in xrange(neurons): 96 # phi[i, j] = metrics(x[i,:], mu[j])**3) 97 # phi[i, j] = plateSpine(x[i,:], mu[j])) 98 # phi[i, j] = invMultiQuadric(x[i,:], mu[j], sigmas[j])) 99 phi[i, j] = multiQuadric(x[i,:], mu[j], sigmas[j]) 100 # phi[i, j] = gaussian(x[i,:], mu[j], sigmas[j])) 101 if k % 1000 == 0: 102 percent = true_divide(k, s)*100 103 print(c, ': {:2.2f}%'.format(percent)) 104 print(c, ': Done') 105 106 # distributing the work between 4 workers 107 shared_array = Array(c_double, n * neurons) 108 phi = frombuffer(shared_array.get_obj()) 109 phi = phi.reshape((n, neurons)) 110 111 jobs = [] 112 workers = [] 113 114 p = n / C 115 m = n % C 116 for c in range(C): 117 jobs.append((c*p, (c+1)*p + (m if c == C-1 else 0))) 118 worker = Process(target = heavy_lifting, args = (c, phi)) 119 workers.append(worker) 120 worker.start() 121 122 for worker in workers: 123 worker.join() 124 125 return phi 126 127 def _do_algebra(self, y): 128 phi = self.phi 129 130 w = lstsq(phi, y)[0] 131 os = dot(w, transpose(phi)) 132 return w, os 133 # Saving to HDF5 134 os_h5 = os_handle.create_dataset('os', data = os) 135 136 def train(self, x, y): 137 self.n = x.shape[0] 138 139 ## Initialize HDF5 caches 140 prefix = self.prefix 141 postfix = str(self.n) + '-' + str(self.extra_neurons) + '.hdf5' 142 name_template = prefix + '-{}-' + postfix 143 phi_handle = self.phi_handle = File(name_template.format('phi'), 'w') 144 os_handle = self.w_handle = File(name_template.format('os'), 'w') 145 w_handle = self.w_handle = File(name_template.format('w'), 'w') 146 mu_handle = self.mu_handle = File(name_template.format('mu'), 'w') 147 sigma_handle = self.sigma_handle = File(name_template.format('sigma'), 'w') 148 149 ## Mu generation 150 mu = self.mu = self._generate_mu(x) 151 self.neurons = mu.shape[0] 152 print('({} neurons)'.format(self.neurons)) 153 # Save to HDF5 154 mu_h5 = mu_handle.creat。

pythonreadhdf5

转载请注明出处编程代码网 » pythonreadhdf5(好玩的电脑小代码)

资讯

pythonrequests解析(pythonrequests库怎么解析响应)

阅读(14)

本文主要为您介绍pythonrequests解析,内容包括pythonrequests库怎么解析响应,python怎么解析requests.session.get,python:Request的函数是什么作用。Requests 是使用 Apache2 Licensed 许可证的 HTTP 库。用 Python 编写,真正的为人类着想

资讯

pythonnumpy函数库(pythonnumpy有什么用)

阅读(10)

本文主要为您介绍pythonnumpy函数库,内容包括python有numpy函数库么,python有numpy函数库么,python中numpy内容。NumPy is the fundamental package for scientific computing with Pytho

资讯

macos安装python(如何在mac上安装python)

阅读(13)

本文主要为您介绍macos安装python,内容包括如何在mac上安装python,macos怎么安装python,如何在MacOSX上安装Python3.3。在mac上搭建python环境: mac系统其实自带了一个python的执行执行环境,用来运行python还行,但是开发可能就不够了,因

资讯

pythonisselected(python出现这个错误是什么原因)

阅读(12)

本文主要为您介绍pythonisselected,内容包括该怎么办,python/django/html:如何输出selected,python出现这个错误是什么原因。要把代码发现来才知道,以下是常见的错误 下面终于要讲到当你用到更多的Python的功能(数据类型,函数,模块,类等等)时可能

资讯

python编译成dll(python有没有办法能够编译成DLL文件呢?)

阅读(13)

本文主要为您介绍python编译成dll,内容包括python程序怎么编译成dll,python程序怎么编译成dll,python有没有办法能够编译成DLL文件。我在工作中就是用python些程序,再用c\c++封装掉 给同事调用的具体的做法是1. python 程序用 py2exe 打包(

资讯

pythondecode参数(python的instr.decode在哪个库)

阅读(11)

本文主要为您介绍pythondecode参数,内容包括python怎么使用decode,python怎么使用decode,pythondecode中文编码。问题: Dim SearchString, SearchChar, MyPos SearchString ="XXpXXpXXPXXP"