我使用Opencv Python获得了一些视频帧,并且能够将其转换为灰度。
现在我想做这样的事情来计算ssim_multiscale
i1 = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
i2 = cv2.cvtColor(frame2,cv2.COLOR_BGR2GRAY)
i1 = np.array(i1, dtype=np.float64)
i2 = np.array(i2,dtype=np.float64)
print(tf.image.ssim_multiscale(i1,i2,255))
但是我遇到了这个错误
shape1 = img1.get_shape()。with_rank_at_least(3)。 AttributeError:“ numpy.ndarray”对象没有属性“ get_shape”
我检查了np.array的文档,它具有get_shape属性。如何将Mat传递给ssim_multiscale参数?
tf.image.ssim_multiscale(
img1,
img2,
max_val,
power_factors=_MSSSIM_WEIGHTS
)
答案 0 :(得分:0)
最后找到答案
package **.*****.**;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import org.apache.http.HttpHost;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.net.Proxy;
import android.os.Build;
import android.os.Parcelable;
import android.util.ArrayMap;
import static freedom.theanarch.org.freedom.Main.*;
/**
* Created by brad on 8/28/17.
*/
public class WebkitProxy {
private final static int DEFAULT_SOCKS_PORT = 9050;
public static boolean setProxy(String host, int port)throws Exception{
setSystemProperties(host, port);
boolean worked = false;
if(Build.VERSION.SDK_INT < 13){
setProxyUpToHC(host, port);
}else if(Build.VERSION.SDK_INT < 19){
worked = setWebkitProxyICS(host, port);
}else if(Build.VERSION.SDK_INT < 20){
worked = setKitKatProxy(activity.getApplicationContext(), host, port);
if(!worked){
worked = setWebkitProxyICS(host, port);
}
}else if(Build.VERSION.SDK_INT >= 21){
worked = setWebkitProxyLollipop(activity.getApplicationContext(), host, port);
}
return worked;
}
public static void resetProxy()throws Exception{
resetSystemProperties();
if(Build.VERSION.SDK_INT < 13){
resetProxyForGingerBread(activity.getApplicationContext());
}else if(Build.VERSION.SDK_INT < 19){
resetProxyForICS();
}else if(Build.VERSION.SDK_INT < 20){
setKitKatProxy(activity.getApplicationContext(), "", 0);
}else if(Build.VERSION.SDK_INT >= 21){
setWebkitProxyLollipop(activity.getApplicationContext(), "", 0);
}
}
private static void setSystemProperties(String host, int port){
System.setProperty("proxyHost", host);
System.setProperty("proxyPort", Integer.toString(port));
System.setProperty("http.proxyHost", host);
System.setProperty("http.proxyPort", Integer.toString(port));
System.setProperty("https.proxyHost", host);
System.setProperty("https.proxyPort", Integer.toString(port));
System.setProperty("socks.proxyHost", host);
System.setProperty("socks.proxyPort", Integer.toString(DEFAULT_SOCKS_PORT));
System.setProperty("socksProxyHost", host);
System.setProperty("socksProxyPort", Integer.toString(DEFAULT_SOCKS_PORT));
}
private static void resetSystemProperties(){
System.setProperty("proxyHost", "");
System.setProperty("proxyPort", "");
System.setProperty("http.proxyHost", "");
System.setProperty("http.proxyPort", "");
System.setProperty("https.proxyHost", "");
System.setProperty("https.proxyPort", "");
System.setProperty("socks.proxyHost", "");
System.setProperty("socks.proxyPort", Integer.toString(DEFAULT_SOCKS_PORT));
System.setProperty("socksProxyHost", "");
System.setProperty("socksProxyPort", Integer.toString(DEFAULT_SOCKS_PORT));
}
@SuppressWarnings("all")
private static boolean setProxyUpToHC(String host, int port){
HttpHost proxyServer = new HttpHost(host, port);
Class networkClass = null;
Object network = null;
try{
networkClass = Class.forName("android.webkit.Network");
if(networkClass == null){
return false;
}
Method getInstanceMethod = networkClass.getMethod("getInstance", Context.class);
network = getInstanceMethod.invoke(networkClass, new Object[]{activity.getApplicationContext()});
}catch(Exception ex){
return false;
}
if(network == null){
return false;
}
Object requestQueue = null;
try{
Field requestQueueField = networkClass.getDeclaredField("mRequestQueue");
requestQueue = getFieldValueSafely(requestQueueField, network);
}catch(Exception ex){
return false;
}
if(requestQueue == null){
return false;
}
Field proxyHostField = null;
try{
Class requestQueueClass = Class.forName("android.net.http.RequestQueue");
proxyHostField = requestQueueClass.getDeclaredField("mProxyHost");
}catch(Exception ex){
return false;
}
boolean temp = proxyHostField.isAccessible();
try{
proxyHostField.setAccessible(true);
proxyHostField.set(requestQueue, proxyServer);
}catch(Exception ex){
}finally{
proxyHostField.setAccessible(temp);
}
return true;
}
private static Object getFieldValueSafely(Field field, Object classInstance)throws IllegalArgumentException, IllegalAccessException{
boolean oldAccessibleValue = field.isAccessible();
field.setAccessible(true);
Object result = field.get(classInstance);
field.setAccessible(oldAccessibleValue);
return result;
}
private static boolean setWebkitProxyICS(String host, int port){
try{
Class webViewCoreClass = Class.forName("android.webkit.WebViewCore");
Class proxyPropertiesClass = Class.forName("android.net.ProxyProperties");
if(webViewCoreClass != null && proxyPropertiesClass != null){
Method m = webViewCoreClass.getDeclaredMethod("sendStaticMessage", Integer.TYPE, Object.class);
Constructor c = proxyPropertiesClass.getConstructor(String.class, Integer.TYPE, String.class);
if(m != null && c != null){
m.setAccessible(true);
c.setAccessible(true);
Object properties = c.newInstance(host, port, null);
m.invoke(null, 193, properties);
return true;
}
}
}catch(Exception e){
}
return false;
}
@TargetApi(19)
private static boolean setKitKatProxy(Context appContext, String host, int port){
if(host != null){
System.setProperty("http.proxyHost", host);
System.setProperty("http.proxyPort", Integer.toString(port));
System.setProperty("https.proxyHost", host);
System.setProperty("https.proxyPort", Integer.toString(port));
}
try{
Field loadedApkField = Main.class.getField("mLoadedApk");
loadedApkField.setAccessible(true);
Object loadedApk = loadedApkField.get(appContext);
Class loadedApkCls = Class.forName("android.app.LoadedApk");
Field receiversField = loadedApkCls.getDeclaredField("mReceivers");
receiversField.setAccessible(true);
ArrayMap receivers = (ArrayMap) receiversField.get(loadedApk);
for(Object receiverMap : receivers.values()){
for(Object rec : ((ArrayMap) receiverMap).keySet()){
Class clazz = rec.getClass();
if(clazz.getName().contains("ProxyChangeListener")){
Method onReceiveMethod = clazz.getDeclaredMethod("onReceive", Context.class, Intent.class);
Intent intent = new Intent(Proxy.PROXY_CHANGE_ACTION);
if(host != null){
final String CLASS_NAME = "android.net.ProxyProperties";
Class cls = Class.forName(CLASS_NAME);
Constructor constructor = cls.getConstructor(String.class, Integer.TYPE, String.class);
constructor.setAccessible(true);
Object proxyProperties = constructor.newInstance(host, port, null);
intent.putExtra("proxy", (Parcelable) proxyProperties);
}
onReceiveMethod.invoke(rec, appContext, intent);
}
}
}
return true;
}catch(Exception e){
e.printStackTrace();
}
return false;
}
@TargetApi(21)
@SuppressWarnings("rawtypes")
private static boolean setWebkitProxyLollipop(Context appContext, String host, int port){
System.setProperty("http.proxyHost", host);
System.setProperty("http.proxyPort", Integer.toString(port));
System.setProperty("https.proxyHost", host);
System.setProperty("https.proxyPort", Integer.toString(port));
try{
Class applictionClass = Class.forName("android.app.Application");
Field mLoadedApkField = applictionClass.getDeclaredField("mLoadedApk");
mLoadedApkField.setAccessible(true);
Object mloadedApk = mLoadedApkField.get(appContext);
Class loadedApkClass = Class.forName("android.app.LoadedApk");
Field mReceiversField = loadedApkClass.getDeclaredField("mReceivers");
mReceiversField.setAccessible(true);
ArrayMap receivers = (ArrayMap) mReceiversField.get(mloadedApk);
for(Object receiverMap : receivers.values()){
for(Object receiver : ((ArrayMap) receiverMap).keySet()){
Class clazz = receiver.getClass();
if(clazz.getName().contains("ProxyChangeListener")){
Method onReceiveMethod = clazz.getDeclaredMethod("onReceive", Context.class, Intent.class);
Intent intent = new Intent(Proxy.PROXY_CHANGE_ACTION);
onReceiveMethod.invoke(receiver, appContext, intent);
}
}
}
return true;
}
catch(Exception e){
e.printStackTrace();
}
return false;
}
private static void resetProxyForICS()throws Exception{
try{
Class webViewCoreClass = Class.forName("android.webkit.WebViewCore");
Class proxyPropertiesClass = Class.forName("android.net.ProxyProperties");
if(webViewCoreClass != null && proxyPropertiesClass != null){
Method m = webViewCoreClass.getDeclaredMethod("sendStaticMessage", Integer.TYPE, Object.class);
if(m != null){
m.setAccessible(true);
m.invoke(null, 193, null);
}
}
}catch(Exception e){
e.printStackTrace();
}
}
private static void resetProxyForGingerBread(Context ctx)throws Exception{
Object requestQueueObject = getRequestQueue(ctx);
if(requestQueueObject != null){
setDeclaredField(requestQueueObject, "mProxyHost", null);
}
}
public static Object getRequestQueue(Context ctx)throws Exception{
Object ret = null;
Class networkClass = Class.forName("android.webkit.Network");
if(networkClass != null){
Object networkObj = invokeMethod(networkClass, "getInstance", new Object[] { ctx }, Context.class);
if(networkObj != null){
ret = getDeclaredField(networkObj, "mRequestQueue");
}
}
return ret;
}
private static Object getDeclaredField(Object obj, String name)throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException{
Field f = obj.getClass().getDeclaredField(name);
f.setAccessible(true);
Object out = f.get(obj);
return out;
}
private static void setDeclaredField(Object obj, String name, Object value)throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException{
Field f = obj.getClass().getDeclaredField(name);
f.setAccessible(true);
f.set(obj, value);
}
private static Object invokeMethod(Object object, String methodName, Object[] params, Class... types)throws Exception{
Object out = null;
Class c = object instanceof Class ? (Class) object : object.getClass();
if(types != null){
Method method = c.getMethod(methodName, types);
out = method.invoke(object, params);
}else{
Method method = c.getMethod(methodName);
out = method.invoke(object);
}
return out;
}
}
如果使用打印功能打印ms-ssim值,它将显示奇怪的错误