net.sf.json.JSONObject对象使用指南

article/2025/9/23 18:27:43

1 简介

在程序开发过程中,在参数传递,函数返回值等方面,越来越多的使用JSON。JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,同时也易于机器解析和生成、易于理解、阅读和撰写,而且Json采用完全独立于语言的文本格式,这使得Json成为理想的数据交换语言。
JSON建构于两种结构:

  1. “名称/值”对的集合(A Collection of name/value pairs),在不同的语言中,它被理解为对象(Object), 记录(record), 结构(struct), 字典(dictionary), 有趣列表(keyed list), 哈希表(hash table)或者关联数组(associative array)。
  2. 值得有序列表(An order list of values)。在大多数语言中,它被理解为数组(array).
    在C++程序设计中,可以使用RapidJson、Jsoncpp等工具,因为才开始从事Java程序的开发,为了提升开发效率,因此也要熟练掌握一种Json生成和解析的工具。在Java中较为常见的Json解析工具有阿里巴巴的fastjson,但由于项目中自带有net.sf.json包。因此使用该包进行Json的构建和解析。

2 详细描述

JSON-lib是一个用于转换beans、maps、collections、java arrays、XML成JSON对象以及反之转化成benas和DynaBeans的java库。JSON-lib建立在Douglas Crockford的工作基础之上。

2.1 JSONObject

public final class JSONObject extends AbstractJSON implements JSON, Map, Comparable {

从上述的接口声明中,可以看到JSONObject实现了接口Map,Comparable,这两个接口为java jdk自带的接口,而JSON接口代码如下:

package net.sf.json;
import java.io.Serializable;
import java.io.Writer;public interface JSON extends Serializable {boolean isArray();boolean isEmpty();int size();String toString(int var1);String toString(int var1, int var2);Writer write(Writer var1);}

一个JSONObject是一个无序的name/value对集合。它的外部形式是一个由花括号“{}”括起的字符串,在name和value之间使用冒号“:”隔开,在names/values之间用逗号隔开。JSONObjet的图示如下:
这里写图片描述
如下例所示:

{"errCode": 200,"msg": "success","data": {"jguan": "11","xming": "11","dwei_name": "","zji_code": "","is_cbdwfzltxiu": "","fzltxiu_date": "2",}
}

内部的形式是一个拥有get和opt方法来根据name访问value、put方法来根据name增加或替换value的对象。在JSONObject中,值可以为以下类型:

Boolean、 JSONObject、 JSONArray、 NumberString、 或者JSONNull对象

JSONObject类型的构造器可以用于转换外部形式的JSON text成一种可以由get和opt方法访问的内部形式,或者使用element方法和toString方法把值转换成为JSON text。如果name存在,则get会返回对应的值,否则会抛出异常JSONException。而opt方法则会在不存在所要检索的name时可以指定一个默认值而不是抛出异常,这样可以获取可选择值。
普通的get()和opt()方法会返回Object,因而可以对返回值进行强制类型转换。也可以使用类型化的get和opt方法进行类型审核和强转。
String myString = new JSONObject().put(“JSON”, “Hello, World!”).toString();
将会产生{“JSON”: “Hello, World”}

2.2 JSONArray

JSONArray是一个有序的值得序列。它的外部形式是包括在方括号[]内的字符串,由逗号分隔值。

[“aaa”, “bbb”, “ccc”]

内部的形式是一个拥有get和opt方法的对象,这两个方法可以根据索引访问值,element方法可以用来添加和替换值。
这里写图片描述
这些值可以是任意类型

Boolean、 JSONObject、 JSONArray、 NumberString、 或者JSONNull对象

图示如下:
这里写图片描述
JSONArray的类型定义如下:

public final class JSONArray extends AbstractJSON implements JSON, List, Comparable {

因此在操作JSONArray对象时可以使用JSON接口、List接口以及Comparable接口提供的方法,可以把JSONArray看成一个List对象。
构造器可以把一个JSON text转换成为Java对象,toString方法转换成JSON text。

3 如何安装

可以使用Maven快速导入该net.sf.json包,对应放入pom.xml依赖说明如下:

<dependency><groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>

在pom.xml文件中添加上述的依赖,Import Changes即可。

注意:添加的依赖文件必须含有标记,不然maven无法从仓库中下载成功

4 JSONObject

4.1 构造器

JSONObject有两种常见的构造方法,一般常用的构造函数为JSONObject(),构造一个不含任何name/value的JSONObject对象

JSONObject
public JSONObject()
Construct an empty JSONObject. JSONObject
public JSONObject(boolean isNull)
Creates a JSONObject that is null. 

4.2 判断方法

由于JSONObject继承自Map接口,因此该类型自动的包含一些判断方法,简要介绍如下:

4.2.1 判空

判空的含义是JSONObject对象中是否不包含任何name/value对映射。

public boolean isEmpty()
Description copied from interface: JSON
Returns true if this object has no elements or keys. 
Specified by:
isEmpty in interface Map
Specified by:
isEmpty in interface JSON

<>4.2.2 判Null
判Null则对应构造函数的第二种形式,即该对象为null,而相应的在这类对象的基础上,如果添加了键值对,则会重新分配空间。

isNullObject
public boolean isNullObject()
Returs if this object is a null JSONObject. 

4.2.3 包含name

public boolean containsKey(uObject key)
Specified by:
containsKey in interface Map

该方法确保JSONObject中是否包含键的判断。
JSONObject对象也提供了has方法

public boolean has(String key)
Determine if the JSONObject contains a specific key. 
Parameters:
key - A key string. 
Returns:
true if the key exists in the JSONObject.

4.2.4 包含value

该接口继承自Map接口

public boolean containsValue(Object value)
Specified by:
containsValue in interface Map

4.2.5 判等

两个JSONObject对象相等表示两个JSONObject包含相同的键集合,同时每个键对应的值也相等。

public boolean equals(Object obj)
Specified by:
equals in interface Map
Overrides:
equals in class Object

4.2.6 判断是否为数组

判断一个name对应的value类型是否为数组的方法声明如下:

public boolean isArray()
Description copied from interface: JSON
Returns true if this object is a JSONArray, false otherwise. 
Specified by:
isArray in interface JSON

4.3 存储name/value

在JSONObject中可以存放name/value的方法有如下几种:accumulate方法、element方法、put方法、putAll方法、在此简要介绍这几个方法的用处。

4.3.1 追加

在JSONObject中由于name的类型为字符串类型,而值可以为JSONObject,JSONArray,Boolean、 JSONObject、 JSONArray、 Number、 String、 或者JSONNull对象。
在同一个name下,放置多个值,即组成JSONArray类型。
在下面的方法中,在一个key下累加一个值,如果已经有一个值为存储在key对应的对象中,该函数与element方法类似,此时会在key对应的位置存储一个JSONArray对象保存所有积累的值。如果已经有一个JSONArray对象,那么新值会追加到该JSONArray对象。作为对比,replace方法会替换之前的值。

public JSONObject accumulate(String key,boolean value)
Accumulate values under a key. It is similar to the element method except that if there is already an object stored under the key then a JSONArray is stored under the key to hold all of the accumulated values. If there is already a JSONArray, then the new value is appended to it. In contrast, the replace method replaces the previous value. 
Parameters:
key - A key string.
value - An object to be accumulated under the key. 
Returns:
this.  
Throws: 
JSONException - If the value is an invalid number or if the key is null.

通过accumulate类方法可以把value累加到对应的name下而不会覆盖原有的value值而是形成一个JSONArray对象,不断追加新的Value。
类似的方法如下:

public JSONObject accumulate(String key, boolean value);
public JSONObject accumulate(String key, double value);
public JSONObject accumulate(String key, int value) ;
public JSONObject accumulate(String key, long value) ;
public JSONObject accumulate(String key, Object value);
public JSONObject accumulate(String key, Object value, JsonConfig jsonConfig) ;
public void accumulateAll(Map map);
public void accumulateAll(Map map, JsonConfig jsonConfig) ;

accumulate方法代码片段如下;

JSONArray array = new JSONArray();
array.add("123");
array.add(1);
Map<String, Integer> val = new HashMap<>();
val.put("1", 20);
val.put("2", 30);
System.out.println(array.toString());
array.add(val);
System.out.println(array.toString());JSONObject json = new JSONObject(true);
System.out.println(json.isNullObject());
//Can't accumulate on null object
//json.accumulate("1", 1);
//null object
//json.put("1", 1);
json = new JSONObject();
json.accumulate("1", 1);
json.accumulate("1", val);
System.out.println(json.toString());
json.putAll(val);
System.out.println(json.toString());
打印结果如下:
["123",1]
["123",1,{"1":20,"2":30}]
true
{"1":[1,{"1":20,"2":30}]}
{"1":20,"2":30}

阅读上述代码片段,可以看到几个注意点:

注意:如果使用构造器new
JSONObject(true);则构造出来的JSONObject对象无法accumulate和put。否则会抛出异常,由代码片段中的注释可以看出。
另外put方法会进行替换而不会追加到name对应的value集合中。 最后的json.putAll(val); 中有name
“1”,替换了之前”1”对应的集合。

4.3.2 替换或存储

在JSONObject中替换name对应的value时,可以使用put方法实现

JSONObject json = new JSONObject();
SONArray jsonInner = new JSONArray();
//java.lang.IndexOutOfBoundsException: Index: 5, Size: 0//jsonInner.add(5, "1");
for(int i=0; i<5; i++) {jsonInner.add(i);
}
json.put("1", jsonInner);
System.out.println(json.toString());
json.put("1", new int[]{1, 2, 6});
System.out.println(json.toString());
Map<String, String> vals = new HashMap<String, String>();
vals.put("1", "sqh");
vals.put("3", "lww");
vals.put("2", "computer");
json.putAll(vals);
System.out.println(json.toString());

向JSONObject对象中存储name/value对可以通过put存放。该接口继承自Map接口
当name已经存在于JSONObject中,实际作用是用新值替换旧值,与以下函数等价
json.replace(“1”, “sqh”);

4.3.3 存储

还有一类增加的方法为elemnent方法

public JSONObject element(String key,double value)
Put a key/double pair in the JSONObject. 
Parameters:
key - A key string.
value - A double which is the value. 
Returns:
this. 
Throws: 
JSONException - If the key is null or if the number is invalid.

该类方法与put方法类似,但由于element方法的返回值类型为JSONObject,因此可以使用链式存储的方式,非常方便。
element方法代码片段如下:

JSONObject json = new JSONObject();
json.element("1", 1).element("2", 2);
System.out.println(json.toString());
json.element("1", 3).element("2", 4);
System.out.println(json.toString());

该代码片段运行结果如下:

{"1":1,"2":2}
{"1":3,"2":4}

从代码执行来看,第一次打印的值和第二次打印的值,分析得到实际执行取代了name ”1”和”2”的值。实际方法实现如下:

public JSONObject element(String key, int value) {this.verifyIsNull();return this.element(key, (Object)(new Integer(value)));
}

上述为JDK代码对应的代码实现,存放时转化为(Object)类型。

public JSONObject element(String key, boolean value) ;
public JSONObject element(String key, Collection value);
public JSONObject element(String key, Collection value, JsonConfig jsonConfig);
public JSONObject element(String key, double value) ;
public JSONObject element(String key, int value) ;
public JSONObject element(String key, long value);
public JSONObject element(String key, Map value) ;
public JSONObject element(String key, Map value, JsonConfig jsonConfig);
public JSONObject element(String key, Object value) ;
public JSONObject element(String key, Object value, JsonConfig jsonConfig) ;

4.4 检索

从JSONObject检索数据也提供了一些常用的方法,例如一些列的get方法,opt方法。介绍如下:

get
public Object get(Object key)
Specified by:
get in interface Map
________________________________________
get
public Object get(String key)
Get the value object associated with a key. 
Parameters:
key - A key string. 
Returns:
The object associated with the key. 
Throws: 
JSONException - if this.isNull() returns true.

4.4.1 getXXX

上述的两个方法第一个方法继承自Map接口,因此key的类型为Object,返回值类型也为Object,由于返回值类型为根类型,因此需要强制转换为具体的类型。

getBoolean
public boolean getBoolean(String key)
Get the boolean value associated with a key. 
Parameters:
key - A key string. 
Returns:
The truth. 
Throws: 
JSONException - if the value is not a Boolean or the String "true" or "false".

getBoolean方法如果key为null,或者值不是Boolean或者串”true”或”false”,会抛出JSONException异常。通过getXXX可以获取指定类型的Value而不需要强制转换,较为方便,
getBoolean方法实际实现如下:

public boolean getBoolean(String key) {
this.verifyIsNull();
Object o = this.get(key);
if (o != null) {if (o.equals(Boolean.FALSE) || o instanceof String && ((String)o).equalsIgnoreCase("false")) {return false;}if (o.equals(Boolean.TRUE) || o instanceof String && ((String)o).equalsIgnoreCase("true")) {return true;}}throw new JSONException("JSONObject[" + JSONUtils.quote(key) + "] is not a Boolean.");
}

同类型的方法如下:

public boolean getBoolean(String key) ;
public double getDouble(String key) {
this.verifyIsNull();Object o = this.get(key);if (o != null) {try {return o instanceof Number ? ((Number)o).doubleValue() : Double.parseDouble((String)o);} catch (Exception var4) {throw new JSONException("JSONObject[" + JSONUtils.quote(key) + "] is not a number.");}} else {throw new JSONException("JSONObject[" + JSONUtils.quote(key) + "] is not a number.");}
}public int getInt(String key) ;
public JSONArray getJSONArray(String key) ;
public JSONObject getJSONObject(String key);
public long getLong(String key) ;
public String getString(String key) ;

4.4.2 optXXX

JSONObject同时提供了optXXX的方法获取name对应的value值。

public Object opt(String key) ;
public boolean optBoolean(String key) ;
public boolean optBoolean(String key, boolean defaultValue) ;
public double optDouble(String key) ;
public double optDouble(String key, double defaultValue);
public int optInt(String key) ;
public int optInt(String key, int defaultValue) ;
public JSONArray optJSONArray(String key) ;public JSONObject optJSONObject(String key) {this.verifyIsNull();Object o = this.opt(key);return o instanceof JSONObject ? (JSONObject)o : null;
}public long optLong(String key);
public long optLong(String key, long defaultValue) ;
public String optString(String key) ;
public String optString(String key, String defaultValue);

optXXX的方法与getXXX最大的区别是,optXXX方法可以指定默认值。

optDouble
public double optDouble(String key,double defaultValue)
Get an optional double associated with a key, or the defaultValue if there is no such key or if its value is not a number. If the value is a string, an attempt will be made to evaluate it as a number. 
Parameters:
key - A key string.
defaultValue - The default. 
Returns:
An object which is the value.

4.5 遍历

在遍历JSONObject对象时,需要使用下述的方式。确保正确使用返回值,即迭代器或者Set集合,或者JSONArray

public Iterator keys()
Get an enumeration of the keys of the JSONObject. 
Returns:
An iterator of the keys.
public Set keySet()
Specified by:
keySet in interface Map
public JSONArray names()
Produce a JSONArray containing the names of the elements of this JSONObject. 
Returns:
A JSONArray containing the key strings, or null if the JSONObject is empty.
public Set entrySet()
Specified by:
entrySet in interface Map
public Collection values()
Specified by:
values in interface Map

使用上述方法的代码片段如下:

JSONObject json = new JSONObject();
json.element("1", new int[]{1, 2, 3}).element("2", (new JSONArray()).element(1).element(2));
json.element("3", 5.6);
json.element("4", "abc");
for (Object str:json.keySet()) {String key = (String)str;System.out.println(json.get(key));
}Set<Object> ss = json.entrySet();
for (Object o:ss) {System.out.println(o);
}
Collection<Object> col = json.values();
System.out.println(col);
Iterator<Object> it = col.iterator();
while(it.hasNext()) {//取出迭代器对应的值System.out.println(it.next());
}

4.6 Map或Bean转换成JSONObject

//从一个Map构造JSONObject
Map map = new HashMap();
map.put( "name", "json" );
map.put( "bool", Boolean.TRUE );
map.put( "int", new Integer(1) );
map.put( "arr", new String[]{"a","b"} );
map.put( "func", "function(i){ return this.arr[i]; }" );
JSONObject jObject = JSONObject.fromObject( map );
System.out.println( jObject );
// prints ["name":"json","bool":true,"int":1,"arr":["a","b"],"func":func

从Bean转换为JSONObject

class MyBean{private String name = "json";private int pojoId = 1;private char[] options = new char[]{'a','f'};private String func1 = "function(i){ return this.options[i]; }";private JSONFunction func2 = new JSONFunction(new String[]{"i"},"return this.options[i];");
}
getset方法
//从一个Java对象(Java Bean构造JSONObject)JSONObject jsonObject = JSONObject.fromObject( new MyBean() );System.out.println( jsonObject );
/* prints{"name":"json","pojoId":1,"options":["a","f"],"func1":function(i){ return this.options[i];},"func2":function(i){ return this.options[i];}}
*/

5 代码演示

5.1 JSONObject

JSONObject的演示代码如下:
public class JSONObjectUse {public static void main(String[] args) {//创建JSONObject对象JSONObject jsonObject = new JSONObject();jsonObject.put("username","wln");jsonObject.put("password","123");System.out.println("1:" + jsonObject);//增加属性jsonObject.element("sex","男");System.out.println("2:" + jsonObject);//判断输出对象的类型boolean isArray = jsonObject.isArray();boolean isEmpty = jsonObject.isEmpty();boolean isNullObject = jsonObject.isNullObject();System.out.println("3:" + "是否是数组:" + isArray +" 是否是空:" + isEmpty + " 是否是空对象:" + isNullObject);//创建JSONArrayJSONArray jsonArray = new JSONArray();jsonArray.add(0,"aa");jsonArray.add("BB");jsonArray.add(1,"AB");jsonArray.add("cc");//将JSONArray 添加到JSONObjectjsonObject.element("student",jsonArray);System.out.println("4:" + jsonObject);}
}

结果:

1:{"username":"wln","password":"123"}
2:{"username":"wln","password":"123","sex":"男"}
3:是否是数组:false 是否是空:false 是否是空对象:false
4:{"username":"wln","password":"123","sex":"男","student":["aa","AB","BB","cc"]}

5.2 JSONArray

public class JSONArrayUse {public static void main(String[] args) {//创建JSONArray对象JSONArray jsonArray = new JSONArray();jsonArray.add(0,"aa");jsonArray.add(1,"BB");jsonArray.element("cc");jsonArray.add("DD");System.out.println("1:" + jsonArray);//根据下标获取数据System.out.println("2:" + jsonArray.get(0));//根据下标设置数据jsonArray.set(0,"AAA");System.out.println("3:" + jsonArray);//创建JSONObjectJSONObject jsonObject = new JSONObject();jsonObject.put("username", "lwc");jsonObject.put("password", "123");//把JSONObject放入到JSONArray中jsonArray.add(jsonObject);System.out.println("4:" + jsonArray);//遍历System.out.println("5:");for(int i=0;i<jsonArray.size();i++){System.out.print(jsonArray.get(i)+"\t");}}
}

结果:

1:["aa","BB","cc","DD"]
2:aa
3:["AAA","BB","cc","DD"]
4:["AAA","BB","cc","DD",{"username":"lwc","password":"123"}]
5:
AAA    BB    cc    DD    {"username":"lwc","password":"123"}

5.3 Spring-MVC中用于参数接受

在使用JSONObject进行Java Web开发时,常用的便是json参数的解析和传送。在下述的代码片段中可以使用url进行访问:

url:localhost:8080/netjson?a=1&b=2

同时使用postman工具指定url Body为

{   "name":"sqh",    "pwd":"123",    "phone":1234 }

想要以一种非常方便的方式接受参数,则可以如下设置:

public JSONObject sfJsonTest(HttpServletRequest request, @RequestBody JSONObject content) {

通过指定@RequestBody类型为JSONObject,可以完成从JSON text到JSONObject的自动转换。

package com.example.demo;import net.sf.json.JSONObject;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;import javax.servlet.http.HttpServletRequest;@RestController
public class NetSfJsonTest {@ResponseBody@RequestMapping("/netsfjson")public JSONObject sfJsonTest(HttpServletRequest request, @RequestBody JSONObject content) {System.out.println("?后的内容: "+request.getQueryString());String name = (String)content.get("name");Integer phone = (Integer)content.get("phone");String pwd = (String)content.get("pwd");JSONObject result = new JSONObject();result.put("errCode", 200);result.put("errMsg", "success");result.put("data", content);return result;}
}

6 总结

由于在项目开发时必须以json的格式接受并且向前端以json的方式回传数据,因此比较熟悉某种json解析的方式非常有帮助。在C++开发时较为详细的了解了rapidJSON工具的使用,而Java开发时net.sf.json包的使用可以扮演同样的角色,熟练这个使用可以提升开发的效率。
文档位于

https://download.csdn.net/download/lk142500/10652591

7 引用

www.json.org/json-zh.html
https://www.cnblogs.com/nananana/p/9263708.html


http://chatgpt.dhexx.cn/article/ZM74EBZu.shtml

相关文章

@JsonFormat、@JSONField、@DateTimeFormat详细解说

三者出处 1、JsonFormat来源于jackson&#xff0c;Jackson是一个简单基于Java应用库&#xff0c;Jackson可以轻松的将Java对象转换成json对象和xml文档&#xff0c;同样也可以将json、xml转换成Java对象。Jackson所依赖的jar包较少&#xff0c;简单易用并且性能也要相对高些&a…

Django JSONField类型操作解析

Django JSONField类型操作解析 模型代码设计正向查询与反向查询解析Json字段操作解析新增查询Json条件查询字段条件查询跨关系查询 修改删除 接口测试平台核心以Httprunner为接口用例运行框架&#xff0c;要将用例的数据持久化到数据库中&#xff0c;方便读取修改与存储&#x…

【java】属性别名:@JsonProperty和@JSONField的区别?【图文教程】

平凡也就两个字: 懒和惰; 成功也就两个字: 苦和勤; 优秀也就两个字: 你和我。 跟着我从0学习JAVA、spring全家桶和linux运维等知识&#xff0c;带你从懵懂少年走向人生巅峰&#xff0c;迎娶白富美&#xff01; 关注微信公众号【 IT特靠谱 】&#xff0c;每一篇文章都是心得总结…

fastjson @JSONField format 不生效的原因

&#xff08;以下问题已在fastjson高版本优化<目前我用的是1.2.83版本>&#xff09; 在一次接手项目中发现&#xff0c;http接口返回json数据&#xff0c;Date类型的变量用JSONField format注解格式化不起作用。排查原因&#xff1a; 1&#xff09;查http接口序列化是不…

formdata和json

HTTP content-type Content-Type&#xff08;内容类型&#xff09;&#xff0c;一般是指网页中存在的 Content-Type&#xff0c;用于定义网络文件的类型和网页的编码&#xff0c;决定浏览器将以什么形式、什么编码读取这个文件&#xff0c;这就是经常看到一些 PHP 网页点击的结…

常用注解@JsonField、@JsonFormat、@DateTimeFormat区别

JsonFormat 该注解来源于jackson包中的注解&#xff0c;主要用来控制后端返回给前端的日期格式&#xff0c;通常用在返回给前端的实体类中。 案例如下&#xff1a; class User{private Integer id;JsonFormat(pattern”yyyy-MM-dd”,timezone”GMT8”)private Date birthday;…

Springboot中使用@JsonProperty和@JSONField

2个注解都是为了解决json字符串的某些属性名和JavaBean中的属性名匹配不上的问题。 例子&#xff0c;不使用注解的情况 Data public class Routine {private Integer TTS_voice;} PostMapping("/test8")public Routine test8(RequestBody Routine routine){retur…

@JSONField

1.引入依赖 <dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.76</version></dependency> 2.JSONField注解可以用在方法&#xff08;method&#xff09;&#xff0c;属性&#xff0…

@JsonField 不起作用

在Springboot中默认的JSON解析框架是jackson&#xff0c;引入alibaba的fastjson&#xff0c;使用JSONField 去接收带有native的json请求&#xff0c;实体类名需要做一个转换&#xff0c;但是接受json串的时候&#xff0c;用到JsonField不起作用&#xff0c;接受过来还是null。 …

一眼看清@JSONField注解使用与效果

JSONField是做什么用的 JSONField是fastjson的一个注解&#xff0c;在fastjson解析一个类为Json对象时&#xff0c;作用到类的每一个属性&#xff08;field&#xff09;上。 通过用JSONField注解一个类的属性&#xff0c;我们可以达到以下目标 指定field对应的key名称&#…

perclos

WierwiIIe驾驶模拟器上的实验结果证明&#xff0c;眼睛的闭合时间一定程度地反映疲劳&#xff0c; 如图 所示。 在此基础上&#xff0c; 卡内基梅隆研究所经过反复实验和论证&#xff0c;提出了度量疲劳/瞌睡的物理量 PERCLOS &#xff08;Percentage of EyeIid CIosure over t…

机器视觉毕业设计 深度学习驾驶人脸疲劳检测系统 - python opencv

文章目录 0 前言1 课题背景2 Dlib人脸识别2.1 简介2.2 Dlib优点2.3 相关代码2.4 人脸数据库2.5 人脸录入加识别效果 3 疲劳检测算法3.1 眼睛检测算法3.2 打哈欠检测算法3.3 点头检测算法 4 PyQt54.1 简介4.2相关界面代码 0 前言 &#x1f525; 这两年开始毕业设计和毕业答辩的…

Android studio开发 课设必备疲劳检测app

使用android内置摄像头&#xff0c;可以根据人的状态判定是否疲劳&#xff0c;界面设计较好&#xff0c;检测后还可以回到主界面 运行截图如下&#xff0c;获取源码有偿&#xff0c;拒绝白嫖党~

基于MATLAB的人眼开度疲劳检测识别

一、课题背景 目前&#xff0c;随着人们生活水平的提高&#xff0c;各国人民汽车保有量也越来越多&#xff0c;伴随而来的是交通事故也在不断增多。研究表明&#xff0c;疲劳驾驶是造成交通事故日益严重的重要原因。开展驾驶员疲劳检测和预警的研究工作&#xff0c;有着十分重…

Dlib+Opencv库实现疲劳检测

文章目录 1.关键点检测2.算法实现的核心点3.算法实现&#xff08;1&#xff09;人脸的关键点集合&#xff08;2&#xff09;加载人脸检测库和人脸关键点检测库&#xff08;3&#xff09;绘制人脸检测的框&#xff08;4&#xff09;对检测之后的人脸关键点坐标进行转换&#xff…

Python基于OpenCV的工作疲劳检测系统[源码&UI界面&部署教程]

1.图片演示 2.视频演示 [项目分享]Python基于OpenCV的实时疲劳检测[源码&#xff06;演示视频&#xff06;部署教程]_哔哩哔哩_bilibili 3.检测方法 1&#xff09;方法 与用于计算眨眼的传统图像处理方法不同&#xff0c;该方法通常涉及以下几种组合&#xff1a; 1、眼睛定…

Opencv之疲劳检测

项目要求 在一段视频中&#xff0c;通过检测人眨眼的次数来判断他的疲劳程度。 代码实现 1、导入工具包 from scipy.spatial import distance as dist import numpy as np import dlib import cv22、对脸上的部位进行定义 在关键点定位的官方文档中&#xff0c;提取68个关…

人脸识别-驾驶疲劳检测(1) 眨眼检测

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 目录 前言 一、背景 &#xff08;1&#xff09;环境搭建 &#xff08;2&#xff09;下载开源数据集 &#xff08;3&#xff09;视觉疲劳检测原理 二、代码示例 三、效…

人脸检测高级:疲劳检测

今天我们实现疲劳检测。 如果眼睛已经闭上了一段时间&#xff0c;我们会认为他们开始打瞌睡并发出警报来唤醒他们并引起他们的注意。我们测试一段视频来展示效果。同时代码中保留开启摄像头的的代码&#xff0c;取消注释即可使用。 使用 OpenCV 构建犯困检测器 要开始我们的实…

数字图像处理课程设计-疲劳检测系统

文章目录 数字图像处理课程设计-疲劳检测系统前言一、课程设计任务二、设计框图三、准备工作四、任务流程&#xff14;.&#xff11;视频预处理4.2图片分割 五、结果六、项目总结 数字图像处理课程设计-疲劳检测系统 前言 此系统基于MATLAB设计,核心思想是PERCLOS算法. 参考文…